Getting each array inside brackets in associative array

I have grouped an associative array and stored the items into a variable to send to a JS file from PHP. The associative array looks like this and is pulling two different JSON from a two separate tables in a database:

[{"year":"2020"},{"year":"2021"},{"year":"2022"}],[{"year":"2020"},{"year":"2021"},{"year":"2022"}]

I am trying to access each object like this

[{"year":"2020"}]

I can console.log each JSON individually but not each object. I have added a foreach loop but it just console logs each character. How do I access each object individually?

This is my JS

// This gives me an array with each associative array but I am needing to get each object inside each associative array
var dynamicDBJSON = lfCreatorData.lfJSON;
var stringIt = JSON.stringify(dynamicDBJSON);
var obj = JSON.parse(stringIt);
var abc = (obj);
console.log(abc);



// When I add the for each it console.logs either every grouped associative array or an individual character.
// Loop through each data JSON
Object.keys(abc).forEach(key => {
    console.log(abc);
});
Object.keys(abc).forEach(key => {
    console.log(abc[key]);
});

What is the output of

console.log(abc)

?

The output is one array. This separates the two associative arrays but doesn’t give me access to each array.

[{"year":"2020"}`,{"year":"2021"},{"year":"2022"}]

I need to further separate this array so that I can console.log each associative array separately.

{"year":"2020"}
{"year":"2021"}
{"year":"2022"}
____________
{"year":"2020"}
{"year":"2021"}
{"year":"2022"}

Note the square brackets:

[{"year":"2020"},{"year":"2021"},{"year":"2022"}],-->[{"year":"2020"},{"year":"2021"},{"year":"2022"}]

I need to console log just like this except instead of one array in the console I need two arrays printed in console. Right now I can get one array printed or I can print every single character of both arrays. Sorry if this is confusing.

Basically, in lamens terms, I need to console log each item in the array but separate each array. Using the square brackets to signify the start of a new array.

Yes this is confusing :slight_smile:

Can you show us what you like to output?



They are very similar so literally, I need to print both of the arrays into the console.

I am grouping JSONS I have stored in a database because it is sent to my JS file only once. The JSONS are now accessible in my JS file but I am having a hard time separating them since they share the same variable name.

var myCombinedJSONVar = [{“year”:“2020”},{“year”:“2021”},{“year”:“2022”}],[{“year”:“2020”},{“year”:“2021”},{“year”:“2022”}]

I need something like for each bracket separated array in myCombinedJSONVar console.log each key value pair.

I think the real issue is that is it not registering as a valid JSON because there are multiple JSONS inside one object. So, that’s why I am needing a way to split these two JSONS. It’s possible this approach leads nowhere.

You can surround your string with {} to become a valid JSON object

I actually had already tried that and when I attempted to parse the JSON it said unexpected comma ,

Surrounded with square brackets? e.g. nested arrays.

JSON.parse('[[{"year":"2020"},{"year":"2021"},{"year":"2022"}],[{"year":"2020"},{"year":"2021"},{"year":"2022"}]]')

// outputs
(2) [Array(3), Array(3)]
0: (3) [{…}, {…}, {…}]
1: (3) [{…}, {…}, {…}]
length: 2

Yes, but I need to have each array separated.

So 0: would be
{…}
{…}
{…}
and 1: would be
{…}
{…}
{…}

I’m feeding the data to my creator function and if I try to send the entire array then it doesn’t work. But if I separate them it does.

Neither PHP nor jScript have a JSON type.
The data from your php file is just two text strings.
That the strings are in JSON format is immaterial, they are just text.
Take each string and put them into an array.
Then json_encode the array
I may be wrong but I think that concentating two JSON formatted strings does not create valid JSON.

The solution was handled directly in PHP. I needed to use a foreach loop and then push each array into a global array using array_push. Lastly, I used json_encode to send the variable to my JS file to run the rest of my functions with the data.

There were a couple of different ways to handle this such as a nested for each loop but that just seemed like it wasn’t efficient.

If you intend to handle a few of the nested objects and you know their keys then use the keys to access them otherwise iteration is the best method.
Use a forEach method to iterate the array and a for…in to iterate the object

arrayOfObjects.forEach(obj => {
    for(let key in obj) {
        console.log(`${key}: ${obj[key]}`);
    }
});