繁体   English   中英

将对象键添加到对象数组中的每个项目

[英]Add object key to each item in object array

我有一个从这样的端点返回的有效负载:

const charges = [
    {
        'id': 'someId',
        'dates': ['2017-11-11', '2017-12-11'],
    },
    {
        'id': 'anotherId',
        'dates': ['2017-09-11', '2017-10-11'],
    },
];

id附加到dates数组中的每个项目的最佳方法是什么,以便产生以下结果:

[
    { id: 'someId', date: '2017-11-11' },
    { id: 'someId', date: '2017-12-11' },
    { id: 'anotherId', date: '2017-09-11' },
    { id: 'anotherId', date: '2017-10-11' },
]

我尝试过这样的事情:

let history = [];
charges.map(charge => (
    charge.dates.map((date) => (
        history.concat({
            id: charge.payerCode,
            date: date,
        })
    ))
));

但是历史并没有在我使用它的范围内定义。

我知道可能有更好的方法来做到这一点,但我似乎无法绕过它。

任何帮助将不胜感激!

谢谢!

您可以在连接所有对象时使用Array#reduce ,使用日期数组构建。

 let charges = [{ id: 'someId', dates: ['2017-11-11', '2017-12-11'] }, { id: 'anotherId', dates: ['2017-09-11', '2017-10-11'] }], result = charges.reduce((r, { id, dates }) => r.concat(dates.map(date => ({ id, date }))), []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

边缘版本,它可以防止(它可能是Edge内部的一个bug)

 SCRIPT5113: Use before declaration result = charges.reduce((r, { id, dates }) => r.concat(dates.map(date => ({ id, date }))), []); ^ 

 const charges = [{ id: 'someId', dates: ['2017-11-11', '2017-12-11'] }, { id: 'anotherId', dates: ['2017-09-11', '2017-10-11'] }], result = charges.reduce((r, a) => r.concat(a.dates.map(d => ({ id: a.id, date: d }))), []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

 const charges = [ { 'id': 'someId', 'dates': ['2017-11-11', '2017-12-11'], }, { 'id': 'anotherId', 'dates': ['2017-09-11', '2017-10-11'], }, ]; function flatten(charges) { return charges.reduce((p, {id, dates}) => (dates.forEach(date => p.push({id, date})), p), []); } console.log(flatten(charges)) 

您可以将Array#reduceArray#concat结合使用,如下所示:

 const charges = [ { 'id': 'someId', 'dates': ['2017-11-11', '2017-12-11'], }, { 'id': 'anotherId', 'dates': ['2017-09-11', '2017-10-11'], }, ]; var r = charges.reduce(function(acc, obj) { return acc.concat(obj.dates.map(function(date) { return { id : obj.id, date : date }; })) }, []); console.log(r); 

一个简单的必要解决方案是:

const history = [];
for (const { payerCode, dates } of charges) {
  for (const date of dates) {
    history.push({ id: payerCode, date });
  }
}

尝试使用Array#reduce重新创建数组,使用Array#forEach迭代内部数组

 const charges = [{'id': 'someId', 'dates': ['2017-11-11', '2017-12-11'],}, { 'id': 'anotherId','dates': ['2017-09-11', '2017-10-11'],},]; var res = charges.reduce(function(a,b){ b.dates.forEach(function(n){ a.push({id:b.id,dates:n}) }) return a; },[]) console.log(res) 

ES6

 const charges = [{'id': 'someId', 'dates': ['2017-11-11', '2017-12-11'],}, { 'id': 'anotherId','dates': ['2017-09-11', '2017-10-11'],},]; var res = charges.reduce((a,b) =>(b.dates.forEach(d=>a.push({id:b.id,dates:d})),a),[]) console.log(res) 

$(document).ready(function(){
    const charges = [
    {
        'id': 'someId',
        'dates': ['2017-11-11', '2017-12-11'],
    },
    {
        'id': 'anotherId',
        'dates': ['2017-09-11', '2017-10-11'],
    },
];
var temp={};
var finalArray = [];
for(ch in charges){
    for(dt of charges[ch]['dates']){
        temp.id = charges[ch]['id'];
        temp.date =dt;
        finalArray.push(temp);
        temp={};
    }
}
console.log(JSON.stringify(finalArray));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM