0

My question is quite similar to this one: Merge keys array and values array into an object in JavaScript

However, I don't seem to find the solution for my example. If I have these two arrays:

const keys = ['x', 'y', 'z'];

const values = [
  [0, 1, 2],
  [10, 20, 30],
];

How do I combine them into an array of objects, such that would be the expected result?

[
  {
    x: 0,
    y: 1,
    z: 2,
  },
  {
    x: 10,
    y: 20,
    z: 30,
  },
]
2
  • And the problem is? Just do the steps from the linked question values.length times (values.reduce(<magic>)) Commented May 21, 2021 at 8:32
  • 1
    values.map(x => x.reduce((a,c,i) => (a[keys[i]] = c, a), {}) would be my very quick attempt Commented May 21, 2021 at 8:35

3 Answers 3

1

You can use Array.prototype.map() and Array.prototype.reduce() for this:

const keys = ['x', 'y', 'z'];

const values = [
  [0, 1, 2],
  [10, 20, 30],
];

const res = values.map(arr => arr.reduce((acc, curr, index) => {
  acc[keys[index]] = curr

  return acc;
}, {x: null, y: null, z: null}));

console.log(res);

Or you can also do it without reduce() like so:

const res = values.map(arr => ({
  [keys[0]]: arr[0],
  [keys[1]]: arr[1],
  [keys[2]]: arr[2]
}));
Sign up to request clarification or add additional context in comments.

1 Comment

(you might want to increment the index in the map, otherwise it will be the same key/value repeated three times)
1

Cycle through the value arrays and create an object for each, then cycle through the values within those arrays and use the keys array as the keys.

const keys = ['x', 'y', 'z'];

const values = [
  [0, 1, 2],
  [10, 20, 30],
];

const output = [];
values.forEach((v, i) => {
  output[i] = {}; // create objects for each of the value arrays
  v.forEach((w, j) => {
    output[i][keys[j]] = w; // use the correct keys with each of the values
  });
});

console.log(output);

As comments have also pointed out, this can be done with Array.reduce:

const keys = ['x', 'y', 'z'];

const values = [
  [0, 1, 2],
  [10, 20, 30],
];

const output = values.map(x => { // for each of the values arrays
  return x.reduce((a, c, i) => { // take its values
    a[keys[i]] = c // map them to an object property one by one
    return a; // put them together in the same object.
  }, {});
});

console.log(output);

Comments

0

const keys = ['x', 'y', 'z'];

const values = [
  [0, 1, 2],
  [10, 20, 30],
];


function toObject(keys, values) {
    let final = []
     for (let i = 0; i < values.length; i++){
         let result = {};
         for(let j=0;j<keys.length;j++){
             result[keys[j]] = values[i][j];
            
         }
        final.push(result)
     }
    return final 
    console.log(final)
     
 }
 
 toObject(keys,values )

it will work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.