Javascript 将阵列中的两个对象合并为阵列中的一个对象
所以我试着把两个对象合并在一起,我的主要问题是我有一个switch语句,它有返回对象,所以当它把它聚集起来时,它会把它作为一个对象推到数组中,我希望所有的键值都推到一个对象中Javascript 将阵列中的两个对象合并为阵列中的一个对象,javascript,loops,object,Javascript,Loops,Object,所以我试着把两个对象合并在一起,我的主要问题是我有一个switch语句,它有返回对象,所以当它把它聚集起来时,它会把它作为一个对象推到数组中,我希望所有的键值都推到一个对象中 let grab = document.data.body.map(function(slice, i) { switch (slice.slice_type) { case 'structure': const styles = slice.primary; return {
let grab = document.data.body.map(function(slice, i) {
switch (slice.slice_type) {
case 'structure':
const styles = slice.primary;
return {
styles
} //<< --Want this bracket object to be the same as the return object below
case 'section':
const headline = slice.primary.headline;
const ids = slice.items.map(function(item) {
return item.templates.id;
});
return { // <<<--- Same Object as above
headline,
ids
};
}
});
我得到的
[
{
styles: {
headline: [Array],
}
},
{
headline: [ [Object] ],
ids:['XV2EzREAACEAmbZ3']
}
]
已更新
所以,你们的建议似乎解决了这个问题,但它只在第一个对象中循环
let grab = document.data.body.map(function (slice, i) {
switch (slice.slice_type) {
case 'structure':
const styles = slice.primary;
return { styles };
case 'section':
const headline = slice.primary.headline;
const ids = slice.items.map(function (item) {
return item.templates.id;
});
return { headline, ids };
}
});
let templates = [Object.assign({}, ...grab)];
console.log(templates, 'temp');
console.log(grab, 'grab'); << what I want as far as grabbing everything, but needs the two objects to merge.
**What I want**
{ styling: [array],
headline: [ [Object] ],
ids:
[ 'XWP3pxMAACMAf2tL',
'XWP34xMAACUAf2xf',
'XWP4MxMAACYAf23U',
'XWP40hMAACQAf3Cq',
'XWP4_xMAACQAf3F7',
'XWP5KRMAACUAf3I5',
'XWP5VxMAACMAf3ML',
'XWP5gxMAACQAf3Pa' ] },
{ styling: [array],
headline: [ [Object] ],
ids:
[ 'XWP1bxMAACQAf2DZ',
'XWP1oRMAACUAf2HL',
'XWP17BMAACYAf2M-',
'XWP2LxMAACMAf2R9',
'XWP2YRMAACYAf2Vl',
'XWP2mRMAACQAf2Zt',
'XWP2zxMAACUAf2dv',
'XWP3DBMAACQAf2iP' ] } ]
{ styling: [array],
headline: [ [Object] ],
ids:
[ 'XWP1bxMAACQAf2DZ',
'XWP1oRMAACUAf2HL',
'XWP17BMAACYAf2M-',
'XWP2LxMAACMAf2R9',
'XWP2YRMAACYAf2Vl',
'XWP2mRMAACQAf2Zt',
'XWP2zxMAACUAf2dv',
'XWP3DBMAACQAf2iP' ] } ] 'grab'
**Whats Happening**
[ {styling: array}
{ headline: [ [Object],
ids:
[ 'XWP1bxMAACQAf2DZ',
'XWP1oRMAACUAf2HL',
'XWP17BMAACYAf2M-',
'XWP2LxMAACMAf2R9',
'XWP2YRMAACYAf2Vl',
'XWP2mRMAACQAf2Zt',
'XWP2zxMAACUAf2dv',
'XWP3DBMAACQAf2iP' ] } ] 'temp'
let grab=document.data.body.map(函数(切片,i){
开关(slice.slice\u类型){
案例“结构”:
常量样式=slice.primary;
返回{style};
案例“部分”:
const headline=slice.primary.headline;
const id=slice.items.map(函数(项){
返回item.templates.id;
});
返回{headline,ids};
}
});
让templates=[Object.assign({},…grab)];
日志(模板“temp”);
log(grab,'grab') 如何结合使用和类似的[Object.assign({},…data)]
?spread操作符将一个iterable(如数组)拆分为多个参数,并将参数值从右向左复制到单个对象中
例如
const数据=[
{a:'a',b:'b'},
{c:'c',d:'d'},
]
console.dir(
[对象.赋值({},…数据)]
)
答案就在这里,因此我创建了一个空数组,然后使用push方法将键值推送到一个对象中
let templates = [];
for (let i = 0; i < documents.body.length; i++) {
let slice = documents.body[i];
if (slice.slice_type === 'structure') {
templates.push({
styles: slice.primary,
headline: [],
ids: []
});
} else if (slice.slice_type === 'section') {
templates.push({
styles: [],
headline: slice.primary.headline,
ids: slice.items.map(item => item.templates.id)
});
}
}
你的输入和代码有点不清楚。您是否正在尝试这样做:尝试使用simple for next循环,而不是map函数。然后你也可以看到引擎盖下到底发生了什么。顺便说一句,为什么要为单个对象创建一个数组?您可以在合并到数组中后放入对象。return{…styles}
将使其与另一个案例中的对象相同。。。
let templates = [];
for (let i = 0; i < documents.body.length; i++) {
let slice = documents.body[i];
if (slice.slice_type === 'structure') {
templates.push({
styles: slice.primary,
headline: [],
ids: []
});
} else if (slice.slice_type === 'section') {
templates.push({
styles: [],
headline: slice.primary.headline,
ids: slice.items.map(item => item.templates.id)
});
}
}
templates = [
{
headline [],
ids: []
},
{
styles: [],
headline: [],
ids: []
}
]