简体   繁体   English

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

[英]Merge two objects in an array into one object in array

So I'm trying to merge two objects together, my main problem is that I have a switch statement that has return objects, so when it gather it up it pushes it into the array as an object and what I want all key values push into one object.所以我试图将两个对象合并在一起,我的主要问题是我有一个带有返回对象的 switch 语句,所以当它收集它时,它会将它作为 object 推送到数组中,我希望所有键值都推送到一个 object。

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

what I want我想要的是

[{ styles: { 
  left_column_headline1: [Array]
  headline: [Array],
  ids: [ 'XV2EzREAACEAmbZ3',]  
}]

What I'm getting我得到了什么

[
 { 
   styles: { 
    headline: [Array],
   }
 },
 {
   headline: [ [Object] ],
   ids:['XV2EzREAACEAmbZ3'] 
 }
]

UPDATED So, It seems your guys suggestions seem to fix it, but it only loops in the first object only.更新所以,看来你们的建议似乎可以解决它,但它只在第一个 object 中循环。

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'

How about using a combination of Object.assign() and the spread operator like [Object.assign({}, ...data)] ?如何结合使用Object.assign()[Object.assign({}, ...data)]之类的扩展运算符 The spread operator splits an iterable (like your array) into multiple parameters and Object.assign() copies parameter values into a single object from right-to-left.扩展运算符将一个可迭代的(如您的数组)拆分为多个参数,并且Object.assign()将参数值从右到左复制到单个 object 中。

For example...例如...

 const data = [ { a:'a', b:'b' }, { c:'c', d:'d' }, ] console.dir( [Object.assign({}, ...data)] )

Here was the answer, So I create an empty array and then I push key values into one single object using the push method.这是答案,所以我创建了一个空数组,然后使用 push 方法将键值推送到一个 object 中。

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

Output Output

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

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

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