wertyj
wertyj

Reputation: 21

Loop not gathering all values from array

I am trying to create 2 loops that work one after each other. I am basically trying to get all the fruits from the array. I have tried doing a for loop inside a for loop but that only gives me the first fruit from each object not every single fruit in the array.

var customers = [{
            "Name" : "John",
            "Items" :[
                {"Fruits" : "Apple"},{"Fruits" : "Orange"}]
            },{
                "Name" : "Sam",
            "Items" :[
                {"Fruits" : "Pear"},{"Fruits" : "Nuts"}]
            },{
                "Name" : "Eric",
            "Items" :[
                {"Fruits" : "Banana"},{"Fruits" : "Raisins"}]
            }];
 for(i=0; i<=customers.length; i++){
 for(a=0; a<=customers.length; a++){
     alert(customers[i]["Items"][a]);
 }
}

Upvotes: 0

Views: 49

Answers (4)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48427

Just use concat method in combination with map and reduce for a easy solution.

let fruits = [].concat(...customers.map(a => a.Items.map(b=>b.Fruits)));

var customers = [{ "Name" : "John", "Items" :[ {"Fruits" : "Apple"},{"Fruits" : "Orange"}] },{ "Name" : "Sam", "Items" :[ {"Fruits" : "Pear"},{"Fruits" : "Nuts"}] },{ "Name" : "Eric", "Items" :[ {"Fruits" : "Banana"},{"Fruits" : "Raisins"}] }];

let fruits = [].concat(...customers.map(a => a.Items.map(b=>b.Fruits)));
console.log(fruits);

If you want to get all unique fruits, you can use Set from ES6.

var customers = [{ "Name" : "John", "Items" :[ {"Fruits" : "Apple"},{"Fruits" : "Orange"}] },{ "Name" : "Sam", "Items" :[ {"Fruits" : "Pear"},{"Fruits" : "Nuts"}] },{ "Name" : "Eric", "Items" :[ {"Fruits" : "Banana"},{"Fruits" : "Raisins"}] }];
let fruits = [...new Set([].concat(...customers.map(a => a.Items.map(b=>b.Fruits))))];
console.log(fruits);

Upvotes: 0

Raavan
Raavan

Reputation: 66

This should solve your issue.

var customers = [{
            "Name" : "John",
            "Items" :[
                {"Fruits" : "Apple"},{"Fruits" : "Orange"}]
            },{
                "Name" : "Sam",
            "Items" :[
                {"Fruits" : "Pear"},{"Fruits" : "Nuts"}]
            },{
                "Name" : "Eric",
            "Items" :[
                {"Fruits" : "Banana"},{"Fruits" : "Raisins"}]
            }];
 for(i=0; i<customers.length; i++){
 for(a=0; a<customers[i]["Items"].length; a++){
     console.log(customers[i]["Items"][a]);
 }
}

Upvotes: 1

vadzim dvorak
vadzim dvorak

Reputation: 959

Your second loop has an incorrect length. Try this:

    var customers = [{
                "Name" : "John",
                "Items" :[{"Fruits" : "Apple"},{"Fruits" : "Orange"}]
            },{
                "Name" : "Sam",
                "Items" :[{"Fruits" : "Pear"},{"Fruits" : "Nuts"}]
            },{
                "Name" : "Eric",
                "Items" :[{"Fruits" : "Banana"},{"Fruits" : "Raisins"}]
            }];
 for(i=0; i<=customers.length; i++){
    for(a=0; a<=customers[i]["Items"].length; a++){
        alert(customers[i]["Items"][a]);
    }
}

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68433

Your second for-loop should be items rather than customers

for(i=0; i < customers.length; i++) //notice that i < instead of i <=
{
   for(a=0; a < customers[i].Items.length; a++) //notice the change here
   {
     alert( customers[i].Items[a].Fruits ); // 
   }
}

A little more precise one would be using reduce

var allFruits = customers.reduce( ( a, b ) => a.concat( b.Items.map( s => s.Fruits ) ) , []);

Upvotes: 2

Related Questions