0

This is my array:

array = [{plant: "shrub", length: "2 meters"},
    {animal: "dog", age: "5 years old"}]

My desired result is:

desiredArray = {plant: "shrub", length: "2 meters", animal: "dog", age: "5 years old" }

Help me combine my array with multiple length.

LeoGarcia
  • 81
  • 1
  • 7

1 Answers1

3

You can use reduce

const array = [
    { plant: 'shrub', length: '2 meters' },
    { animal: 'dog', age: '5 years old' },
];

const result = array.reduce((acc, val) => ({ ...acc, ...val }), {});

console.log(result);
Nikita Madeev
  • 4,284
  • 9
  • 20