代码之家  ›  专栏  ›  技术社区  ›  coderman

JavaScript:为每个具有冗余父属性的嵌套项创建一个新的数组项

  •  -1
  • coderman  · 技术社区  · 8 年前

    [  
       {  
          "suburb":"Collingwood",
          "couples":[  
             {  
                "husband":"Adam",
                "wife":"Brittany"
             },
             {  
                "husband":"Dave",
                "wife":"Carla"
             }
          ]
       },
       {  
          "suburb":"CBD",
          "couples":[  
             {  
                "husband":"Paul",
                "wife":"Christine"
             },
             {  
                "husband":"Mike",
                "wife":"Laura"
             }
          ]
       }
    ]
    

    像这样的数据集

    [  
       {  
          "suburb":"Collingwood",
          "husband":"Adam",
          "wife":"Brittany"
       },
       {  
          "suburb":"Collingwood",
          "husband":"Dave",
          "wife":"Carla"
       },
       {  
          "suburb":"CBD",
          "husband":"Paul",
          "wife":"Christine"
       },
       {  
          "suburb":"CBD",
          "husband":"Mike",
          "wife":"Laura"
       }
    ]
    

    是否有下划线函数来执行类似操作,或者是否需要循环并手动执行。目标是显示按妻子姓名排序和分组的最终列表

    4 回复  |  直到 8 年前
        1
  •  1
  •   Jeto    8 年前

    const result = [].concat(...input.map(entry => entry.couples.map(couple => ({...{suburb: entry.suburb}, ...couple}))));
    

    演示:

    const input = [  
       {  
          "suburb":"Collingwood",
          "couples":[  
             {  
                "husband":"Adam",
                "wife":"Brittany"
             },
             {  
                "husband":"Dave",
                "wife":"Carla"
             }
          ]
       },
       {  
          "suburb":"CBD",
          "couples":[  
             {  
                "husband":"Paul",
                "wife":"Christine"
             },
             {  
                "husband":"Mike",
                "wife":"Laura"
             }
          ]
       }
    ];
    
    const result = [].concat(...input.map(entry => entry.couples.map(couple => ({...{suburb: entry.suburb}, ...couple}))));
    
    console.log(result);
        2
  •  1
  •   slider    8 年前

    reduce 将原始数组转换为具有所需数据格式的新数组:

    var data = [{
        "suburb": "Collingwood",
        "couples": [{
            "husband": "Adam",
            "wife": "Brittany"
          },
          {
            "husband": "Dave",
            "wife": "Carla"
          }
        ]
      },
      {
        "suburb": "CBD",
        "couples": [{
            "husband": "Paul",
            "wife": "Christine"
          },
          {
            "husband": "Mike",
            "wife": "Laura"
          }
        ]
      }
    ];
    
    var res = data.reduce((acc, curr) => {
      // expand each couple to a new object and push all results to accumulator array
      acc.push(...curr.couples.map(c => Object.assign({suburb: curr.suburb}, c)));
      return acc;
    }, []);
    
    console.log(res);
        3
  •  0
  •   Michael Bush    8 年前
    const newArray = oldArray.reduce((result, object) => {
      object.couples.forEach(couple => {
        result.push({
          suburb: object.suburb,
          husband: couple.husband,
          wife: couple.wife
        })  
      })
      return result;
    }, []);
    

    退货:

    [ { suburb: 'Collingwood', husband: 'Adam', wife: 'Brittany' },
      { suburb: 'Collingwood', husband: 'Dave', wife: 'Carla' },
      { suburb: 'CBD', husband: 'Paul', wife: 'Christine' },
      { suburb: 'CBD', husband: 'Mike', wife: 'Laura' } ]
    
        4
  •  0
  •   gavgrif    8 年前

    您可以使用嵌套的forEach循环,首先获取郊区,然后获取成对对象,并将所有对象推送到一个新数组中。

    var data =  [{  
      "suburb":"Collingwood",
      "couples":[
        {"husband":"Adam","wife":"Brittany"},
        {"husband":"Dave","wife":"Carla"}
       ]
      }, {  
      "suburb":"CBD",
      "couples":[
        {"husband":"Paul", "wife":"Christine"},
        {"husband":"Mike","wife":"Laura"}
      ]
    }]
    
    
    var newData=[];
    
    data.forEach(function(item){
     var couples = item.couples;
    
     couples.forEach(function(couple){
      newData.push({
       'suburb': item.suburb,
       'husband': couple.husband,
       'wife': couple.wife
       })
     })
    })
    
    console.log(newData)