Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从阵列中2层以下的对象中选择特性_Javascript_Jquery_Arrays_Dynamic_Javascript Objects - Fatal编程技术网

Javascript 从阵列中2层以下的对象中选择特性

Javascript 从阵列中2层以下的对象中选择特性,javascript,jquery,arrays,dynamic,javascript-objects,Javascript,Jquery,Arrays,Dynamic,Javascript Objects,以下是我的工作内容: 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: '',

以下是我的工作内容:

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
}
我需要到达
对象
中的
道具
s,同时动态排序
数据
对象

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
。我敢肯定,我对选择器感到茫然。这里有快速帮助吗?

这应该可以:

else if (Data[d]['object'].hasOwnProperty(item)){
    SecondObj[item] = Data[d]['object'][item]
}    

这是在数组通知中
object
是一个对象,不是数组,所以它没有[item];属性将位于。item,而不是[item]Hold是数组索引,因此
Data[d]
是一个
Obj
Data[d]['object']
也是一个对象,
item
将是
prop1
所以
Data[d]['object'['prop1']
应该可以工作不?哦,很抱歉混淆了。它确实有效。在某些测试数据和对象名称之间存在一些交叉命名约定。谢谢你,先生!所有的事情都被分类了,负责测试数据的人被解雇了。