使3个数组彼此对应,第一个是对象名称 [英] Make 3 arrays correspond to each other with the first being the object name
本文介绍了使3个数组彼此对应,第一个是对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前有多个看起来像这样的对象:
I currently have multiple objects that look like:
let object1 = {
xyz: 'xyz1',
arr: []
},
object2 = {
xyz: 'xyz2',
arr: []
},
object3 = {
xyz: 'xyz3',
arr: []
}
这变得非常漫长和多余.无论如何,我可以创建3个数组,例如:
This is getting very long and redundant. Is there anyway I can create 3 arrays like:
let objName = ["object1", "object2", "object3"]
let xyzArr = ["xyz1", "xyz2","xyz3"]
let theArr = [[], [], [] ]
反正有对应关系吗?
推荐答案
我不确定您要问的是什么,但这将采用3个数组并将对象存储在window
中:
I'm not sure what you're asking, but this would take the 3 arrays and store the objects in the window
:
let objName = ["object1", "object2", "object3"];
let xyzArr = ["xyz1", "xyz2","xyz3"];
let theArr = [[], [], []];
objName.forEach((name, index) => {
window[name] = {
xyz: xyzArr[index],
arr: theArr[index]
};
});
console.log(object1);
console.log(object2);
console.log(object3);
I would recommend using a namespace instead of making them global (more on that here), but I guess this works?
这篇关于使3个数组彼此对应,第一个是对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文