2016-10-24 21 views
0
data.forEach(function (object, index) { 

    console.debug(object.output); 
    //this consists of array object like below 
    //how to loop through these using higher order function so that I don't have to use for loop 

    [0]{"id": "453", "name":"keren"}, 
    [1]{"id": "453", "name":"amy"}, 
    [2]{"id": "453", "name":"samta"} 

我得到的错误说,object.output不是一个函数。 我喜欢高阶函数的原因是为了避免嵌套for循环获取数组的长度,然后循环通过主数组内的嵌套数组。如何循环更高阶的函数?

即使我使用传统的for循环,我也无法获得object.output的长度。

这里面有什么记录:data.forEach(function (object, index) {....}

enter image description here

现在,我该怎么办环通输出,每一个对象?

基于怪异answwer编辑:

data.forEach(function (object, index) { 
    Object.keys(object).forEach(function (key){ 

     if (typeof object[key] == 'object') { 
      console.debug(object[key]); 
      //how to iterate through each object here? 
      object[key].forEach(function(x){ 
      console.debug(x); 
      }); 
     } 

    }); 
}); 
+0

是'output'应该的属性你正在使用的对象?如果是这样,什么资源证实了这一点? –

+0

@SamOnela,'output'是包含对象数组的对象 – 112233

+0

object.forEach(function(obj){console.log(obj);});这会给你数组中的对象 – Geeky

回答

1

检查以下内容片断

var arr=[ {"id": "453", "name":"keren"} 
 
      ,{"id": "453", "name":"amy"} 
 
      ,{"id": "453", "name":"samta"} 
 
      ] 
 

 
var object={"output":arr} 
 
Traverse(object); 
 

 
function Traverse(object){ 
 
Object.keys(object).forEach(function (key){ 
 
    console.log(object[key]); 
 
    if(typeof(object[key])==="object") 
 
      Traverse(object[key]); 
 
      
 
}); 
 
}

希望它可以帮助

+0

我已经尝试过,并没有帮助,因为它说object.output不是一个函数。 .I截图的数据层次结构,请参考我编辑的文章 – 112233

+0

我已经修改了代码,请检查并确认如果这是你寻找的 – Geeky

+0

现在可以得到的对象,但如何进一步循环通过每个输出对象?请参考我编辑过的帖子上面 – 112233