【问题标题】:Loop not gathering all values from array循环不收集数组中的所有值
【发布时间】:2017-12-12 13:17:12
【问题描述】:

我正在尝试创建 2 个循环,它们一个接一个地工作。我基本上是在尝试从阵列中获取所有水果。我尝试在 for 循环中执行 for 循环,但这只会给我每个对象的第一个水果,而不是数组中的每个水果。

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

【问题讨论】:

    标签: javascript arrays loops object for-loop


    【解决方案1】:

    你的第二个for循环应该是items而不是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 ); // 
       }
    }
    

    更精确一点是使用reduce

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

    【讨论】:

    • 也应该是i &lt;a &lt;而不是i &lt;=a &lt;=
    • 感谢您的帮助!
    【解决方案2】:

    您的第二个循环的长度不正确。试试这个:

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

    【讨论】:

      【解决方案3】:

      这应该可以解决您的问题。

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

      【讨论】:

        【解决方案4】:

        只需将concat 方法与mapreduce 结合使用即可轻松解决。

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

        如果你想得到所有unique的水果,你可以使用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);

        【讨论】:

        • 由于使用的是ES6,可以试试[].concat(...customers.map(...))
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-27
        • 2015-11-09
        • 2014-11-25
        • 1970-01-01
        相关资源
        最近更新 更多