所以这就是我正在使用的内容:
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
}
我需要同时prop
object
同时对Data
和Obj
进行动态排序:
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
。对于选择者而言,我很不确定。这里有快速帮助吗?
答案 0 :(得分:2)
这应该有效:
else if (Data[d]['object'].hasOwnProperty(item)){
SecondObj[item] = Data[d]['object'][item]
}