0

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!

10
  • 9
    Did you try anything? If yes, where did you get stuck? Commented Dec 5, 2017 at 16:41
  • Since all arrays have equal length: "for index from 0 to amount of items in one of my arrays, insert an object into array z such that z[index].x is equal to x[index], z[index].y is equal to y[index] and z[index].w is equal to w[index]" Commented Dec 5, 2017 at 16:44
  • 3
    Who is upvoting this? It is the definition of "lacks effort" Commented Dec 5, 2017 at 16:47
  • This is a solid and interesting question to solve. But please show us what you've already tried that hasn't worked for you, since SO is not a code-writing service. :-) Commented Dec 5, 2017 at 16:47
  • I agree on lack of effort, this is not a kind of problem which requires solid algorithm and data-structure understanding. OP could have made some effort to produce an ugly version. OP hasn't even replied to inquiries from other experts. Commented Dec 5, 2017 at 16:55

5 Answers 5

4

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);

Sign up to request clarification or add additional context in comments.

8 Comments

Sometimes I wonder why low quality questions get answers by high-reputation users that should vote to close them instead.
@FedericoklezCulloca Questions lacking attempts should not be closed. Downvoted maybe, but not closed. There is no category/reason for them in the why should this question be closed? box.
@ibrahimmahrir do you know if there's ever been a discussion about this kind of questions in meta?
@FedericoklezCulloca No I don't. Do you?
@ibrahimmahrir No, I don't, that's why I was asking :)
|
1

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);

Comments

0

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.

Comments

0

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);

Comments

0

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; }

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.