首页 > 解决方案 > 如何创建一个对象数组,其中每个对象有 4 个值?

问题描述

我想创建一个对象数组,其中每个对象都有 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...];

标签: javascriptapi

解决方案


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

推荐阅读