Adam Reed
Adam Reed

Reputation: 59

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.

Upvotes: 1

Views: 1053

Answers (5)

Hussain
Hussain

Reputation: 138

var object=[];

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

Upvotes: 0

Ravi
Ravi

Reputation: 11

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

Upvotes: 0

Parag Bhayani
Parag Bhayani

Reputation: 3330

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

Upvotes: 1

Nitsan Baleli
Nitsan Baleli

Reputation: 5458

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);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386883

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>');

Upvotes: 0

Related Questions