Array elements being listed

I recently encountered an issue while trying to store values in a JavaScript object notation. After some programming, I managed to store all occurrences in a string separated by commas. However, when the term I'm looking for is an array, I receive [object object] as the output, understandably so. My goal is to figure out how to store all items within that array into a variable separated by commas. For instance, if I choose "Time", it should return Dec 9, 1, 2012

I have made progress and can retrieve the value if it's not an array: http://jsbin.com/obehog/3/edit

The complexity arises from the fact that the depth of the arrays may vary, making it impossible to use a loop in every case.

Answer №1

When "Time" is selected, the result appears as an array:

[[{
    "term": "Dec 9",
    "Dec_9": [{
        "count": 1,
        "term": "2012"
    }]
}]]

If you desire a result in the format Dec 9, 1, 2012, you must have a method to convert an object into a string (or string array). Here is some sample code:

function valuesOfObj(obj, result) {
    result = result || [];
    if (typeof obj === 'object') {
        for (var k in obj) {
            if (obj.hasOwnProperty(k)) {
                arguments.callee(obj[k], result);
            }
        }
    } else {
        result.push(obj);
    }
    return result;
}

console.log(valuesOfObj([{
    "term": "Dec 9",
    "Dec_9": [{
        "count": 1,
        "term": "2012"
    }]
}]).join(', ')); // -> Dec 9, 1, 2012

View the complete demonstration here

In addition, what you have accomplished is remarkable. There are other tools available such as jsonselect and JSONQuery. Hopefully, they prove useful to you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP failing to recognize ajax request

My PHP script includes a drop-down box, and when I select an option in the dropdown, it triggers an AJAX call to communicate with another PHP script. In Firebug, I can see that the call is being made and the correct parameters are passed. Within my PHP cod ...

Mapping UVs for partial spheres in Three.JS

In a previous inquiry regarding the mapping of a stereographic projection onto a sphere for virtual reality live-streaming purposes, I delved into UV mapping techniques and successfully achieved a visually stunning result. However, there is one aspect of t ...

Trouble Encountered with Strlen() Feature in C Programming

#include <stdio.h> int main() { char arr[]= {'b', 'a', 'n', 'a', 'n', 'a'}; printf("%d %d", sizeof(arr),strlen(arr)); return 0; } After running this code, I was surprised to see the output ...

The output from the console displays a variety of numbers (11321144241322243122)

Every time I attempt to log the number 11321144241322243122 into the console, it consistently converts to a different number 11321144241322244000. This issue persists both in node.js and the browser console. ...

Is it not odd that there are two '=>' symbols in an arrow function when there is typically only one? What could this possibly signify?

function updateStateValue(name) { return (event) => { setState({ ...state, [name]: event.target.checked }); }; } To view the full code example, visit CodeSandbox. ...

What is the best way to populate a text field in AngularJS with multiple selected values?

Seeking assistance with a dynamic text field value that changes based on an AngularJS post request. I am looking to modify this value through multiple selected options. Here is the HTML code: <tr ng-repeat="opt in myData"> <td> & ...

What is the most efficient method for quickly adding rows to a sqlite database?

I'm currently working on inserting approximately 7000 rows into an Android Galaxy Tab2's sqlite database. Right now, I am parsing a JSON file and using a "for" loop to insert each row into the database. However, this process is taking more than 1 ...

Displaying [object Object] in Angular Material datatable

I am currently working on implementing a datatable component using Express and Firebase DB. Below is the service request data: getText() { return this.http.get<nomchamp[]>(this.url) .map(res => { console.log(res); return res }); ...

The server did not receive the file when using FormData

When uploading a form with a file to my server, the validation process is run and it returns either success or failure. This form is sent using Ajax and FormData. On my test server, all data, including the file, is received without any issues. However, o ...

Utilize a custom attribute in Bootstrap 4 tooltips instead of the standard "title" attribute to define the text displayed in

For my application, I'm utilizing Bootstrap 4 along with tooltips. My objective is to toggle the tooltips individually and only display them upon clicking. The following code allows me to achieve this: <input type="text" id="name" class="form-cont ...

Transform a list separated by commas into an unordered list

Seeking a PHP, Jquery, or JavaScript method to convert comma-separated data into an unordered list. For clarification, I have uploaded a CSV file to WordPress where one section of content is separated by commas and I am looking to display it as a list. A ...

Is there a way to customize the color of the icons on material-table for the actions of onRowAdd, onRowUpdate, and onRowDelete

I recently experimented with the material-table library to perform basic CRUD operations. Utilizing onRowAdd, onRowUpdate, and onRowDelete, I was able to incorporate icons for each function. However, I am interested in changing the color of these icons. Ca ...

"Encountered a type error: The .join method is not recognized as a function

Hello everyone, I've encountered an issue that has me a bit stumped. I recently took over a project and found a problem in which the previous developer was attempting to join an array of strings. Unfortunately, when I try to build the project, I keep ...

Using a foreach loop, generate an associative array from an existing array

I am looking to generate a new associative array by iterating through another array using a foreach loop. The initial array contains only user IDs which will be used to fetch data from a MySQL database. I also aim to include additional elements in the fina ...

Detecting Eyes with the Power of JavaScript and HTML5

Looking for suggestions, steps, or algorithms for performing eye detection on 2D images using JavaScript and HTML5. I have successfully converted RGB to YCbCr color space. Now seeking assistance with eye extraction. function detectEyes(e) { var r, g ...

Using asynchronous method calls to create arrays in ES6

In my current task, I am working on completing the following method: collectAllResults: function (callback) { this.getTotalCount((count) => { // the count typically is 5 let results = [] for (var i = 0; i < count; i++) { th ...

"Server request with ajax did not yield a response in JSON format

http://jsfiddle.net/0cp2v9od/ Can anyone help me figure out what's wrong with my code? I'm unable to see my data in console.log, even though the network tab in Chrome shows that my data has been successfully retrieved. Here is my code snippet: ...

Warning: The React Router v6's Route component is unable to find the origin of the key props

I recently came across an error in my console and I'm unsure which list is causing it. Is there a way for me to trace back the origin of this error so I can pinpoint where to fix it? The error seems to be related to the React Router component, which ...

Develop a method in Objective-C that can accept an array of arguments as its input

Hi there I'm trying to figure out how to create a method in Objective C that can take an array of arguments as parameters, similar to: [NSArray arrayWithObjects:@"A", @"B", nil]; The method declaration for this functionality looks like: + (id)arra ...

How to change the color of the tooltip text in Bootstrap Vue

In an attempt to modify the color of the tooltip text from its default to red https://i.sstatic.net/fF78X.png Any payment made during the night hours (00:00 – 06:00) must be displayed as red. ...