0

I have an array = [[1, 2, 3, 4] [5, 6, 7, 8]]

My goal is to create an object from array. Object should look like this

{ 
   1: 2,
   3: 4,
   5: 6,
   7: 8,
}

So, the question is what's the easiest way to do it. Someone advised me to use destructuring assignment.

IISkullsII
  • 705
  • 1
  • 7
flopinski
  • 23
  • 4

1 Answers1

0

If we suppose that your array has always an even size.

var array = [[1, 2, 3, 4], [5, 6, 7, 8]]
const flatArray = array.flat()

const finalArray = {};
for(let i=0; i < flatArray.length; i=i+2) {
    finalArray[flatArray[i]] = flatArray[i+1]
}
Hedi Zitouni
  • 179
  • 1
  • 8