0

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]);
 }
}
0

4 Answers 4

2

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 ) ) , []);
Sign up to request clarification or add additional context in comments.

2 Comments

Also should be i < and a < rather than the i <= and a <=
Thank you for your help!
1

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

Comments

1

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

Comments

0

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

1 Comment

Since you are using ES6, you can try [].concat(...customers.map(...))

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.