将数组中的两个对象合并为数组中的一个对象

时间:2019-10-24 20:30:03

标签: javascript loops object

所以我试图将两个对象合并在一起,我的主要问题是我有一个带有返回对象的switch语句,所以当它收集起来时,它将它作为对象以及我想要的所有键推入数组值推入一个对象。

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: { 
  left_column_headline1: [Array]
  headline: [Array],
  ids: [ 'XV2EzREAACEAmbZ3',]  
}]

我要得到的东西

[
 { 
   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'

2 个答案:

答案 0 :(得分:0)

如何结合使用Object.assign()spread operator之类的 [Object.assign({}, ...data)] ?散布运算符将可迭代的对象(如数组)拆分为多个参数,Object.assign()从右到左将参数值复制到单个对象中。

例如...

const data = [
  { a:'a', b:'b' },
  { c:'c', d:'d' },
]

console.dir(
  [Object.assign({}, ...data)]
)

答案 1 :(得分:0)

这是答案,所以我创建一个空数组,然后使用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)
        });
      }
    }

输出

  templates = [
      {
       headline [],
       ids: []
      },
      {
       styles: [],
       headline: [],
       ids: []
      }
    ]