'Calculating length of array within an object within an array

I'm having trouble creating a loop that is referencing an array within an object within an array.

   var data_dictionary = [{
    "category": "Food",
    "items": [
        {
            "object": "Apple",
            "price": 2.21

        },
        {
            "object": "Banana",
            "price": 1.12
        }
     ]
  }, {
   "category":  "Goods",
   "items":  [
        {
            "object":  "Lawnmower",
            "price":   25.55
        },
        {
            "object":  "Bicycle",
            "price":   12.21
        }
     ]
  }
]

I would like to run a loop that will return the # of "objects", but I'm running into problems

var result =[] 
var temp = []
  for (var i=0; i < data_dictionary.length; i++) {
        for (var c=0; c < data_dictionary[i].items.length; c++) {
          temp.push (data_dictionary[i].items[c].object)
        }
      }
      result.push (temp.length)

The issue has to do with calculating the length in "c < data_dictionary[i].items.length", and likely in the following line as well.

Ideally, I would like result to be 4, since there are 4 objects.

Thanks for reading.



Solution 1:[1]

your data and code should be

var data_dictionary = [{
    "category": "Food",
    "items": [
        {
            "object": "Apple",
            "price": 2.21

        },
        {
            "object": "Banana",
            "price": 1.12
        }
     ]
  }, {
   "category":  "Goods",
   "items":  [
        {
            "object":  "Lawnmower",
            "price":   25.55
        },
        {
            "object":  "Bicycle",
            "price":   12.21
        }
     ]
  }
]

var result;
var temp = [];
for (var i=0; i < data_dictionary.length; i++) {
    for (var c=0; c < data_dictionary[i].items.length; c++) {
      temp.push (data_dictionary[i].items[c].object)
    }
}
result = temp.length

Solution 2:[2]

With a proper data structure (there is one }, { missing, as well as some strings are not terminated), you could build an array or just count the occurence.

var data_dictionary = [{ "category": "Food", "items": [{ "object": "Apple", "price": 2.21 }, { "object": "Banana", "price": 1.12, }] }, { "category": "Goods", "items": [{ "object": "Lawnmower", "price": 25.55 }, { "object": "Bicycle", "price": 12.21 }] }],
    result = [];

data_dictionary.forEach(function (a) {
    a.items.forEach(function (b) {
        result.push(b.object);
    });
});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Solution 3:[3]

if I understood you correctly, you wanted the output as an array with inner arrays lengths.

first you should properly format your data to be valid, then you can iterate over it using the .filter() function for instance:

var data_dictionary = [ {
    "category": "Food",
    "items": [
        {
            "object": "Apple",
            "price": '?2.21'
        },
        {
            "object": "Banana",
            "price": '1.12'
        }
     ]
  },
  {
   "category":  "Goods",
   "items":  [
        {
            "object":  "Lawnmower",
            "price":   '25.55'
        },
        {
            "object":  "Bicycle",
            "price":   '12.21'
        }
     ]
  }];


var result = data_dictionary.map(function(n) {
  return n.items.length;
})

console.log(result);

Solution 4:[4]

In your code missing the } and {, create separate object like below.

var data_dictionary = [{"category":"Food",
 "items":[
 {"object":"Apple",
  "price":"?2.21"
 },
 {"object":"Banana",
 "price":"1.12"
 }
 ]},

{"category":"Goods",
 "items":[{
  "object":"Lawnmower",
 "price":"25.55"
 },     
{
"object":"Bicycle",
"price":"12.21"
}]
}]
var $data_dictionary = $(data_dictionary);

You can use jQuery to write simple code.

var result = []; 
$data_dictionary.each(function(index,elt){
$(elt.items).each(function(itemIndex, itemElt){
 result.push(itemElt);
});});   

here you will get 4 objects

Solution 5:[5]

var object=[];

for(i = 0 ; i < data_dictionary.length; i++)
{
  object.push({'items':data_dictionary[i].object, 'itemsLength':data_dictionary[i].items.length});
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Parag Bhayani
Solution 2 Nina Scholz
Solution 3 Nitsan Baleli
Solution 4 Ravi
Solution 5 Hussain