[英]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#reduce與Array#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.