1

I want to have a JSON object which contains multiple person objects. Is there a better way to format this? What I did below is have a result key which would be an array consisting of arrays (which have the JSON person object inside of the inner array).

{
result: [
            [
              {
                "name": "Josh",
                "age": 15
              }
            ],

            [
              {
                "name": "Joe",
                "age": 16
              }
            ]
       ]
}

Then in the for loop I do something like this:

var nameArray = result[0];
for (var i = 0; i < numberOfObjectsInJSONObject; i++) {
    newNameArray.push(nameArray[i].firstPerson);        
}
4
  • not sure why you have an array of arrays that have a single item. result could be a simple array of objects (p.s. these are objects, not JSON. JSON may be how you retrieve the data, but this isn't JSON you are working with) Commented Nov 20, 2016 at 22:38
  • It's to separate out the different person objects. Like I want to loop through all of them using a for loop. Like each array inside of the result array is one person object. Commented Nov 20, 2016 at 22:39
  • you're wrapping each object in an array containing only that object, that seems useless Commented Nov 20, 2016 at 22:40
  • I understand what you are doing, but having the "inner" array with a single item every time is pointless Commented Nov 20, 2016 at 22:41

3 Answers 3

4

No point in having each person object wrapped inside it's own array. Unless there is some other grouping that needs done all you should need is one array that contains all the person objects

result: [

          {
            "name": "Josh",
            "age": 15
          },
          {
            "name": "Joe",
            "age": 16
          }

   ]

This array can be sorted by ages or names, filtered etc

Sign up to request clarification or add additional context in comments.

4 Comments

How would I loop through the result array then to access each person object individually?
Loop is much simpler now result.forEach(function(person){ console.log(person.name) })
Oh haha I'm stupid. Each position in the array is an object. I was overthinking this too much. Thank you!
Yup I think the light bulb just went off.
1

If you want to keep object like that, something like this work. pleases try this one

var pList = {
  people: [{
    'name': 'Josh',
    'age': 15
  }, {
    'name': 'Joe',
    'age': 16
  }],
  animal: [{
    'type': 'Dog',
    'legs': 4
  }, {
    'type': 'Bird',
    'legs': 2
  }]
};

[].forEach.call(pList.people, function(p) {
    // can push its value into new array or else
    console.log(`${p.name} -- ${p.age}`);
  })
  //or
pList.animal.forEach(function(a) {
  console.log(`${a.type} -- ${a.legs}`)
})

Comments

0

Here is your list of objects:

var a = [];
a.push({ "name" : "Joe", "age" : 16 });
a.push({ "name" : "John", "age" : 17 });

Here is how you can create JSON:

JSON.stringify(a);

Here is the result:

[{"name":"John","age":17},{"name":"Joe","age":16}]

Here is how you can iterate through your array:

for (var i = 0; i < a.length; i++) { console.log(a[i]) }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.