Convert arrays of arrays to object with key

  • A+
Category:Languages

I have arrays of arrays which contains something like this:

var values = [[1, 2, 3], [3, 2, 1]] 

I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:

values = [     {'up': 1, 'middle': 2, 'down': 3],     ... ] 

What should I use? This is what Im came up so far:

const object1 = [[1,2,3],[3,2,1]],   object = []  object1.forEach(function(array) {    object.map(value => ({'up': array[0], 'mid': array[1], 'down': array[2]}))  });  console.log(object) 

 


Not very different from what others already did, but a little more elegant:

let arr = [[1, 2, 3], [3, 2, 1]];  let result = arr.map(([up, middle, down]) => ({up, middle, down})); console.log(result);

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: