Javascript - 遍历对象和更新属性

时间:2016-08-12 17:17:36

标签: javascript recursion iteration tree-traversal

我希望有人可以指出我正确的方向。

我有一个像以下一样的JavaScript对象......

var category = [{
    isExpanded: false,
    name: "Electricals",
    subcategories: [{
        isExpanded: false,
        name: "Audio",
        subcategories: null
    }, {
        isExpanded: false,
        name: "Photography",
        subcategories: null
    }]
}, {
    isExpanded: false,
    name: "Telecoms",
    subcategories: [{
        isExpanded: false,
        name: "Mobile",
        subcategories: null
    }, {
        isExpanded: false,
        name: "Sim-cards",
        subcategories: null
    }]
}, {
    isExpanded: false,
    name: "Entertainment",
    subcategories: [{
        isExpanded: false,
        name: "Gaming",
        subcategories: [{
            isExpanded: false,
            name: "Games",
            subcategories: null
        }]
    }, {
        isExpanded: false,
        name: "Sports",
        subcategories: null
    }]
}];

我的目标是遍历每个类别/子类别,并根据我的逻辑设置'isExpandable'的值。有人可以建议我如何遍历对象的每个子类别,记住一个子类别,还有一个子类别等等?

这是我目前的尝试......

function blah() {
  for (var i = 0; i < category.length; i++) {
    if (category[i].subcategories) {
      console.log(category[i]);
      traverseCategoryTree(category[i]);
    }
  }
}

function traverseCategoryTree(category) {
  //iterate each subcategory and update is Expanded. 
}

blah();

2 个答案:

答案 0 :(得分:3)

function traverseCategoryTree(categories) {
  for (var i = 0, len = categories.length; i < len; i++) {
    console.log(categories[i]);
    if (categories[i].subcategories) {
      traverseCategoryTree(categories[i].subcategories);
    }
  }
}    

traverseCategoryTree(category);

答案 1 :(得分:0)

不要重新发明轮子。现在,我们在大多数数据处理中都使用object-scan。一旦将头缠住它,它就会很强大。这是您的示例的工作方式:

const objectScan = require('object-scan');

const modify = (data) => objectScan(['**[*]'], {
  rtn: 'count',
  filterFn: ({ value }) => {
    value.isExpandable = (
      Array.isArray(value.subcategories)
      && value.subcategories.length !== 0
    );
  }
})(data);

const category = [{"isExpanded":false,"name":"Electricals","subcategories":[{"isExpanded":false,"name":"Audio","subcategories":null},{"isExpanded":false,"name":"Photography","subcategories":null}]},{"isExpanded":false,"name":"Telecoms","subcategories":[{"isExpanded":false,"name":"Mobile","subcategories":null},{"isExpanded":false,"name":"Sim-cards","subcategories":null}]},{"isExpanded":false,"name":"Entertainment","subcategories":[{"isExpanded":false,"name":"Gaming","subcategories":[{"isExpanded":false,"name":"Games","subcategories":null}]},{"isExpanded":false,"name":"Sports","subcategories":null}]}];

console.log(modify(category)); // how many matched found
// => 10

console.log(JSON.stringify(category));
// => [{"isExpanded":false,"name":"Electricals","subcategories":[{"isExpanded":false,"name":"Audio","subcategories":null,"isExpandable":false},{"isExpanded":false,"name":"Photography","subcategories":null,"isExpandable":false}],"isExpandable":true},{"isExpanded":false,"name":"Telecoms","subcategories":[{"isExpanded":false,"name":"Mobile","subcategories":null,"isExpandable":false},{"isExpanded":false,"name":"Sim-cards","subcategories":null,"isExpandable":false}],"isExpandable":true},{"isExpanded":false,"name":"Entertainment","subcategories":[{"isExpanded":false,"name":"Gaming","subcategories":[{"isExpanded":false,"name":"Games","subcategories":null,"isExpandable":false}],"isExpandable":true},{"isExpanded":false,"name":"Sports","subcategories":null,"isExpandable":false}],"isExpandable":true}]