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

时间:2018-10-29 22:06:42

标签: javascript jquery underscore.js

我要转换这样的数据集

[  
   {  
      "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 个答案:

答案 0 :(得分:1)

您可以将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);

答案 1 :(得分:1)

作为一个单行代码(请注意,其他答案可能更易于阅读/维护):

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 :(得分:0)

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' } ]

答案 3 :(得分:0)

您可以使用嵌套的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)