首页 > Web开发 > 详细

js递归遍历json

时间:2018-07-12 15:42:18      阅读:203      评论:0      收藏:0      [点我收藏+]
var array = [{
  "type": "people",
  "name": "chen",
  "sex": "男",
  "age": 30,
  "objects": [{
    "type": "people",
    "name": "chen",
    "sex": "男",
    "age": 31
  }, {
    "type": "people",
    "name": "chen",
    "sex": "男",
    "age": 32
  }]
  }, {
      "type": "people",
      "name": "chen",
      "sex": "男",
      "age": 33,
  }, {
      "type": "people",
      "name": "chen",
      "sex": "男",
      "age": 34,
      "objects": [{
      "type": "people",
      "name": "chen",
      "sex": "男",
      "age": 35
      }]
}];
var ageAll = [];
var findAge = function(arr) {
  arr.forEach(function(item) {
    if (item.age) {
      ageAll.push(item.age);
    }
    if (item.objects && item.objects.length > 0) {
      findAge(item.objects);
    }
  });
}
findAge(array);
console.log(ageAll);

  

js递归遍历json

原文:https://www.cnblogs.com/front-boy/p/9299477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!