我正在尝试使用数组中的值引用多个嵌套对象中的值
类似的东西
var object = {
folder_1:{
item: 8
}
};
然后通过按钮函数中的数组中的对象对其进行访问
var button = function (x,y,z) {
this.x = x;
this.y = y;
this.z = z;
};
button.prototype.run = function () {
object[folder_1][item] = this.x;
// expressed like this
object[this.y] = this.x;
};
var b1 = new button(23,[folder_1,item],0);
这是我当前代码的一部分,如您所见,它非常庞大,并且包含大部分针对我的问题的内容,但是它最多只能工作10级。
var vars = {
library_1: {
numbers: 7
}
};
rectMode(CENTER);
textAlign(CENTER,CENTER);
var button = function (x,y,w,h,f,t,gt,ty,Arr) {
//x coordinate
this.x = x;
//y coordinate
this.y = y;
//width
this.w = w;
//height
this.h = h;
//fill
this.f = f;
//text
this.t = t;
//scene to go to
this.gt = gt;
//type
this.ty = ty;
this.Arr = Arr;
this.ArA = [];
};
button.prototype.draw = function () {
fill(this.f);
rect(this.x,this.y,this.w,this.h,5);
fill(0, 0, 0);
text(this.t,this.x,this.y);
if(mouseX>this.x-this.w/2&&mouseX<this.x+this.w/2&&mouseY>this.y-this.h/2&&mouseY<this.y+this.h/2&&vars.mouse.clicked){
//calibrate
{
vars[this.ty[0]][this.ty[1]]-=1;
vars[this.ty[0]][this.ty[1]]+=1;
}
//sadly uncondensed system for changing variables in the system
if(this.Arr===false){
switch(this.ty.length){
case 2:
vars[this.ty[0]][this.ty[1]]=this.gt;
break;
case 3:
vars[this.ty[0]][this.ty[1]][this.ty[2]]=this.gt;
break;
case 4:
vars[this.ty[0]][this.ty[1]][this.ty[2]][this.ty[3]]=this.gt;
break;
case 5:
vars[this.ty[0]][this.ty[1]][this.ty[2]][this.ty[2]][this.ty[4]]=this.gt;
break;
case 6:
vars[this.ty[0]][this.ty[1]][this.ty[2]][this.ty[3]][this.ty[4]][this.ty[5]]=this.gt;
break;
case 7:
vars[this.ty[0]][this.ty[1]][this.ty[2]][this.ty[3]][this.ty[4]][this.ty[5]][this.ty[6]]=this.gt;
break;
case 8:
vars[this.ty[0]][this.ty[1]][this.ty[2]][this.ty[3]][this.ty[4]][this.ty[5]][this.ty[6]][this.ty[7]]=this.gt;
break;
case 9:
vars[this.ty[0]][this.ty[1]][this.ty[2]][this.ty[3]][this.ty[4]][this.ty[5]][this.ty[6]][this.ty[7]][this.ty[8]]=this.gt;
break;
case 10:
vars[this.ty[0]][this.ty[1]][this.ty[2]][this.ty[3]][this.ty[4]][this.ty[5]][this.ty[6]][this.ty[7]][this.ty[8]][this.ty[9]]=this.gt;
break;
}
}
}
};
var f = new button(200,200,50,20,color(255, 0, 0),"=",0,['library_1','numbers'],false);
var draw = function(){
if(vars.mouse.mouseIP === false && vars.mouse.mouseSense === true){
vars.mouse.clicked = true;
vars.mouse.mouseSense = false;
}
background(150,150,150);
f.draw();
//close button
{
fill(255,0,0);
rect(width-25,10,50,20);
fill(255,255,255);
text("X",width-25,10);
if (mouseX>width-50&&mouseY<20&&vars.mouse.clicked) {
self.close();
}
}
vars.mouse.clicked = false;
vars.mouse.mouseIP = false;
cursor(POINT);
};
答案 0 :(得分:0)
尝试更好地解释您的问题,我必须阅读约10遍才能理解您想要什么。
关于解决方案,请使用switch
之类的数组内置函数,而不要使用map
遍历数组。
这里有一个关于我在说什么的例子。将其放在开发工具控制台上以查看结果。
var obj = {
a: {
value: 0,
b: {
value: 0,
c: {
value: 0,
d: {
value: 0,
}
}
}
}
}
function set(value, path) {
var temp = obj;
path && path.map(function (prop, index) {
if ( temp.hasOwnProperty(prop) ) {
if (path.length - 1 === index)
temp[prop] = value;
else
temp = temp[prop];
}
});
}
set(1, ['a', 'value']);
set(2, ['a', 'b', 'value']);
set(3, ['a', 'b', 'c', 'value']);
set(4, ['a', 'b', 'c', 'd', 'value']);
console.log(obj);