如何创建对象数组,其中每个对象都有4个值?

时间:2019-12-08 19:31:31

标签: javascript api

我想创建一个对象数组,其中每个对象都有4个值。全部来自4个不同的数组。数组很长,我不知道该怎么做。我认为这很困难,我一直在寻找vv'小时。

var local=[F.C Barcelona, Real Madrid, Manchester United.....];
var away=[ Manchester City, PSG, Liverpool....];
var matchDay[2,3,4,5....];
var score=[2-0, 0-1, 2-2...];

2 个答案:

答案 0 :(得分:1)

// array to hold the objects
let arr = []
// assuming the four arrays are all the same length
// just pick one to use for the length
for(let i = 0; i < local.length; i++) {
    // create a new object with the 4 fields, one from each array
    // and grab the i'th entry of each array for it
    let obj = {
        local: local[i],
        away:  away[i],
        matchDay: matchDay[i]
        score: score[i]
    };
    arr.push(obj);
}

答案 1 :(得分:0)

不确定要执行的操作是什么,但是类似的方法还是可以的。

> array1.map(function(item, index){
    return {key1: item,
            key2: array2[index],
            key3: array3[index]
           };
  });