file structure

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

其中ij是索引

我如何使用变量而不是文本来使其工作。

我在以下地方尝试过

tmpKeyName = "profile"
keyObject = "nom"

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


谢谢

最佳答案

假设您的数组/对象结构正确,则代码可以正常工作:



// 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]);
  }
}

关于javascript - 如何使用可变内容读取对象数组的内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41091447/

10-10 08:10