var node = {
  cat_deleted: false,
  cat_id: 1,
  cat_level: 0,
  cat_name: "大家电",
  cat_pid: 0,
  children: [{
    cat_deleted: false,
    cat_id: 3,
    cat_level: 1,
    cat_name: "电视",
    cat_pid: 1,
    children: [{
      cat_deleted: false,
      cat_id: 6,
      cat_level: 2,
      cat_name: "曲面电视",
      cat_pid: 3,
    }, {
      cat_deleted: false,
      cat_id: 7,
      cat_level: 2,
      cat_name: "海信",
      cat_pid: 3,
    }]
  },
  {
    cat_deleted: false,
    cat_id: 4,
    cat_level: 1,
    cat_name: "空调",
    cat_pid: 1,
    children: []
  }
  ]
};
var defkeys = [];
// 递归的形式,获取元素下所有的的id,并保存到 defKeys数组中
function getdefkeys(node, arr) {
  if (!node.children) {
    return arr.push(node.cat_id)
  }
  node.children.forEach(item => this.getdefkeys(item, arr))
}
this.getdefkeys(node, defkeys)
console.log(defkeys)
Last Updated:
Contributors: pengrengui