简体   繁体   English

如何将多个数组转换为多个对象的数组?

[英]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! 谢谢!

Use Array#map function and get also the index, which you will use to get the items from the second and third array. 使用Array#map函数并获取索引,您将使用该索引从第二个和第三个数组中获取项目。 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); 

Try 尝试

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

Explanation 说明

  • iterate x using map 使用map迭代x
  • for each index of x , return an object having keys as x , y and z with values from their respective index . 对于x每个索引 ,返回一个对象,该对象的键为xyz ,其值分别来自它们的索引

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

 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 . 您也可以看看这个小提琴

You can use forEach as following : 您可以按以下方式使用forEach

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM