-1

I have an array that looks like this:

[
  {
    value1: {
      something: "something"
    }
  },
  {
    value2: {
      something: "something"
    }
  }
]

And I need to receive something like this:

{
    value1: {
        something: "something"
    },
    value2: {
        something: "something"
    }
}

Thanks in advance!

AdityaParab
  • 7,024
  • 3
  • 27
  • 40

2 Answers2

0

Simply do a reduce on your original array

const data = [
  {
    value1: {
      something: "something"
    }
  },
  {
    value2: {
      something: "something"
    }
  }
]

const result = data.reduce((acc, cur) => {
  const keys = Object.keys(cur);
  keys.forEach(key => {
    if(!acc[key]) {
      acc[key] = {...cur[key]}
    }
  });
  return acc;
}, {});

console.log(result);

/*

{
  value1: { something: 'something' },
  value2: { something: 'something' }
}

*/
AdityaParab
  • 7,024
  • 3
  • 27
  • 40
-1

const something = 3

console.log([{value1: {something}}, {value2: {something}}].reduce((acc,x) => ({...acc,...x})))
Vulwsztyn
  • 2,140
  • 1
  • 12
  • 20