this._formService.arraySteps [j] .profile [i] .nom = newValue;
其中i
和j
是索引
如何使用变量而不是文本来实现此功能。
我已尝试以下
tmpKeyName = "profile"
keyObject = "nom"
this._formService.arraySteps[j][tmpKeyName][i][keyObject];
由于
答案 0 :(得分:2)
假设您的数组/对象结构正确,您的代码将起作用:
// let's assume that this._formService.arraySteps was the following array of objects:
var arraySteps = [
{
profile: [
{nom: "something"}
]
},
{
profile: [
{nom: "something else"}
]
},
{
profile:[
{nom: "something totally different"}
]
}
];
var tmpKeyName = "profile";
var keyObject = "nom";
// Looping through that array:
for(var j = 0; j < arraySteps.length; ++j){
// Looping through the objects in the array:
for(var i = 0; i < arraySteps[j][tmpKeyName].length; ++i){
console.log(arraySteps[j][tmpKeyName][i][keyObject]);
}
}
&#13;