Assign Array of values as keys to another array of values

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Object.assign with spread syntax ... for the parts.
var object = { columns: ["name", "color"], values: [["lion", "yellow"], ["crow", "black"]] },
    result = { data: object.values.map(v => Object.assign(...object.columns.map((c, i) => ({[c]: v[i]})))) };

console.log(result);
ready
Combination of map and reduce
const data = {
   "columns":[
      "name",
      "color"
   ],
   "values":[
      [
         "lion",
         "yellow"
      ],
      [
         "crow",
         "black"
      ]
   ]
}

const result = {data: data.values.map(el => {
  return data.columns.reduce((prev, curr, index) => {
    prev[curr] = el[index]
    return prev
  }, {})
})}

console.log(result)
ready
for..of loop
let obj = {
   "columns":[
      "name",
      "color"
   ],
   "values":[
      [
         "lion",
         "yellow"
      ],
      [
         "crow",
         "black"
      ]
   ]
}

let [res, key, value] = [{data:Array()}, ...obj.columns];

for (let [a, b] of [...obj.values]) res.data.push({[key]:a, [value]:b});

console.log(res);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.