0

所以这就是我正在使用的:

var Data = [Obj, Obj, Obj]
var Obj = {
   string1: 'string',
   string2: 'another string',
   string3: 'different string',
   object: {
      prop1: 'property',
      prop2: 20
   }
   numeric1: 300
}

var SecondObj = {
   string1: '',
   string2: '',
   string3: '',
   prop1: '',
   prop2: undefined,
   numeric1: undefined
}

我需要在同时动态地对两者进行排序时到达props :objectDataObj

for (var d in Data) {//iterate through Data entries
    for (var item in SecondObj){// iterate through first-level 
         if (Data[d].hasOwnProperty(item)){
             SecondObj.item = Data[d][item]
         }
         else if (Data[d]['object'].hasOwnProperty(item)){
             //select the prop of object, which is a property of Obj
             //then set that as the value of the matching property of the SecondObj
         }
    }
}

我尝试了几种不同的方法来选择这些属性,但它们都抛出错误。我显然不能使用'.item'(很明显,但无论如何我都试过了),而且我不能使用+ '.' + item. 只是对选择器不知所措,我很确定。在这里快速帮助?

4

1 回答 1

2

这应该工作:

else if (Data[d]['object'].hasOwnProperty(item)){
    SecondObj[item] = Data[d]['object'][item]
}    
于 2013-07-18T20:12:22.513 回答