0

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.

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.

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'
MikeyNOob
  • 1
  • 4
  • 1
    It's a bit unclear with your input and the code. Are you trying to do something like this: [Merge multiple objects inside the same array into one object](https://stackoverflow.com/questions/27538349) – adiga Oct 24 '19 at 20:34
  • 1
    Instead of a map function try to do it with simple for next loops. Then you can see also what really goes on under the hood. – Thomas Ludewig Oct 24 '19 at 20:34
  • btw, why an array for a single object? you could put the object after merging into an array. – Nina Scholz Oct 24 '19 at 20:38
  • `return { ...styles };` will make it the same as the object in the other `case`... – Heretic Monkey Oct 24 '19 at 20:41

2 Answers2

0

How about using a combination of Object.assign() and the spread operator like [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.

For example...

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

console.dir(
  [Object.assign({}, ...data)]
)
BCDeWitt
  • 4,540
  • 2
  • 21
  • 34
0

Here was the answer, So I create an empty array and then I push key values into one single object using the push method.

  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

  templates = [
      {
       headline [],
       ids: []
      },
      {
       styles: [],
       headline: [],
       ids: []
      }
    ]
MikeyNOob
  • 1
  • 4