jsrobertouimet
jsrobertouimet

Reputation: 103

How to convert multiple arrays into one array of multiple objects?

Given the following arrays:

let x = [a, b, c, d];
let y = [e, f, g, h];
let w = [i, j, k, l];

How to generated a new array of objects that look like that:

let z = [
    {x: a, y: e, w: i},
    {x: b, y: f, w: j},
    {x: c, y: g, w: k},
    {x: d, y: h, w: l}
];

This is what I came up so far:

for(var i; i < x.length; i++) {

    x = x[i];
    y = y[i];
    w = w[i];

    obj = {
        x: x,
        y: y,
        w: w
    };

    z = [];
    z.push(obj);
}

Thanks!

Upvotes: 0

Views: 1899

Answers (5)

Nina Scholz
Nina Scholz

Reputation: 386578

You could use an array with the keys for the object and reduce the array with the arrays.

var x = ['a', 'b', 'c', 'd'],
    y = ['e', 'f', 'g', 'h'],
    w = ['i', 'j', 'k', 'l'],
    keys = ['x', 'y', 'w'],
    result = [x, y, w].reduce(function (r, a, i) {
        a.forEach(function (v, j) {
            r[j] = r[j] || {}
            r[j][keys[i]] = v;
        });
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 0

Renaud is Not Bill Gates
Renaud is Not Bill Gates

Reputation: 2084

You can use forEach as following :

var result = [];
a.forEach((currenValue, index) => {
    result.push({x:x[index], y:b[index], w:c[index]});
});
console.log(result);

Upvotes: 0

Davide Cannizzo
Davide Cannizzo

Reputation: 3134

let items = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l" ];
for (let i = 0; i < items.length; i++)
  Object.defineProperty(window, items[i], {
    value: items[i],
    writable: false
  });
//The code above there is just to define the variables in your arrays.

let x = [a, b, c, d];
let y = [e, f, g, h];
let w = [i, j, k, l];

let keys = [ "x", "y", "w" ]; //This array contains the name for accessing to your arrays.
let dimension = Math.max(...keys.map(function(array) {
  return eval(array).length;
}));
let z = new Array(dimension);
for (let i = 0; i < dimension; i++) {
  let obj = {};
  for (let ii = 0; ii < keys.length; ii++) {
    let key = keys[ii];
    obj[key] = eval(key)[i];
  }
  z[i] = obj;
}

console.log(JSON.stringify(z));

You can also look at this fiddle.

Upvotes: 0

Suren Srapyan
Suren Srapyan

Reputation: 68655

Use Array#map function and get also the index, which you will use to get the items from the second and third array. I used also || if first array has more items that the others.

let x = ['a', 'b', 'c',' d'];
let y = ['e', 'f', 'g', 'h'];
let w = ['i', 'j', 'k', 'l'];

let mapped = x.map((item, index) => ({ x: item, y: y[index] || '', w: w[index] || '' }));

console.log(mapped);

Upvotes: 4

gurvinder372
gurvinder372

Reputation: 68393

Try

var z = x.map( (s,i) => ({ x : x[i], y : y[i], w : w[i] }) );

Explanation

  • iterate x using map
  • for each index of x, return an object having keys as x, y and z with values from their respective index.

Demo

var x = ['a', 'b', 'c', 'd'];
var y = ['e', 'f', 'g', 'h'];
var w = ['i', 'j', 'k', 'l'];

var z = x.map((s, i) => ({
  x: x[i],
  y: y[i],
  w: w[i]
}));

console.log(z);

Upvotes: 1

Related Questions