javascript - 如何创建一个对象数组,其中每个对象有 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...];
解决方案
// 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);
}
推荐阅读
- python - Pandas 根据条件比较两列并获得另一列具有三个不同的推理
- django - 在 Django Celery Queue 中从 Redis 存储和下载文件
- android - Android Room:解析查询中的外键
- python - 设置表示为 Keras 序列模型的 RNN 的初始状态
- asp.net-core - ASP.NET Core Web API FromHeader 绑定 x-api-key 标头
- c++ - 在 WebAssembly 中使用 ngspice 库
- mysql - 编写一个查询,显示系统中最高的平均进度,进度应该四舍五入到小数点后一位
- angular - RxJs - 类型'Observable
' 不可分配给类型 'Observable ' - angular - 授权后获取用户角色
- python - 我似乎无法弄清楚如何更新 tkinter 标签