2017-08-08 105 views
3

我从一个端点回来这样的有效载荷:对象键添加到每个项目对象数组中

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

什么是对dates阵列连接id到每个项目的所以最好的办法这导致这样的:

[ 
    { 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, 
     }) 
    )) 
)); 

但我在使用它的范围中没有定义历史记录。

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

任何帮助将不胜感激!

谢谢!

+0

你想输出什么? –

+0

请告诉我你期望输出的例子是什么? – Microsmsm

+0

将问题添加到期望的输出。抱歉! –

回答

3

你可以Array#reduce而concat所有对象,与日期数组构建。

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

边缘版本,这将阻止(它可能是边缘的内部的一个错误)

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

+0

这工作得很好!非常感谢!! –

+0

@ChrisFrank,不客气! –

1

您可以结合使用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);

0

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)

1

一个简单势在必行的解决办法是:

const history = []; 
for (const { payerCode, dates } of charges) { 
    for (const date of dates) { 
    history.push({ id: payerCode, date }); 
    } 
} 
0
$(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)); 
}); 
3

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