如何使用变量内容来读取对象数组的内容

时间:2016-12-11 21:41:38

标签: javascript arrays json object

file structure

this._formService.arraySteps [j] .profile [i] .nom = newValue;

其中ij是索引

如何使用变量而不是文本来实现此功能。

我已尝试以下

tmpKeyName = "profile"
keyObject = "nom"

this._formService.arraySteps[j][tmpKeyName][i][keyObject];

由于

1 个答案:

答案 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;
&#13;
&#13;