JavaScript获取多层嵌套数组中的所有键值对

3

我有一个这样的对象:

var data = [
 {"id":"36e1e015d703120058c92cf65e6103eb","title":"Alex McGibbon"},
 
 {"id":"60beb5e7d7600200e5982cf65e6103ad","title":"Alex Linde"},
 
 {"subs":[{"id":"62826bf03710200044e0bfc8bcbe5df1","title":"Abel Tuter"}],"id":"63e8479fdb161300bde15901cf96191c","title":"Abdul Waheed"},
 
 {"subs":[{"subs":[{"id":"12826bf03710200044e0bfc8bcbe5db1","title":"Alfonso Griglen"},{"subs":[{"id":"06826bf03710200044e0bfc8bcbe5d8a","title":"Allyson Gillispie"},{"id":"b282abf03710200044e0bfc8bcbe5d28","title":"Allan Schwantd"}],"id":"22826bf03710200044e0bfc8bcbe5dec","title":"Alejandra Prenatt"}],"id":"0a826bf03710200044e0bfc8bcbe5d7a","title":"Adela Cervantsz"},{"id":"4847c4d4d773020058c92cf65e61038e","title":"Alisa Chinoy"},{"id":"71826bf03710200044e0bfc8bcbe5d3b","title":"Aileen Mottern "},{"id":"a8f98bb0eb32010045e1a5115206fe3a","title":"Abraham Lincoln"}],"id":"7c2e6109dbd65300bde15901cf9619b5","title":"Raju Koyagura"}
 
];

console.log(data)

现在我想要检索所有的id值作为一个新的数组,不考虑它位于哪个嵌套级别。

我的期望结果类似于这样:

var result = ['36e1e015d703120058c92cf65e6103eb','60beb5e7d7600200e5982cf65e6103ad','62826bf03710200044e0bfc8bcbe5df1','06826bf03710200044e0bfc8bcbe5d8a','b282abf03710200044e0bfc8bcbe5d28','22826bf03710200044e0bfc8bcbe5dec','0a826bf03710200044e0bfc8bcbe5d7a','4847c4d4d773020058c92cf65e61038e','71826bf03710200044e0bfc8bcbe5d3b','a8f98bb0eb32010045e1a5115206fe3a','7c2e6109dbd65300bde15901cf9619b5'];

console.log(result);

我不知道如何实现它,需要您的帮助。


为什么不先尝试从第一层获取ID,然后通过迭代数组来查看如何完成这个过程呢? - Nina Scholz
2个回答

20
你可以使用JSON.stringify轻松遍历树结构:
const ids = [];
JSON.stringify(data, (key, value) => {
  if (key === 'id') ids.push(value);
  return value;
});

1
我本来想建议将其转换为字符串并搜索正则表达式,但是你的答案(如果它真的有效)是更好的解决方案。 - NoOorZ24
简单而强大的替代编写自定义递归的方法。 - Asinus Rex

3
创建一个递归函数并检查该对象是否有一个按id键。将id的值推进去。如果键是另一个数组,则使用新值调用相同的函数。

var data = [{
    "id": "36e1e015d703120058c92cf65e6103eb",
    "title": "Alex McGibbon"
  },

  {
    "id": "60beb5e7d7600200e5982cf65e6103ad",
    "title": "Alex Linde"
  },

  {
    "subs": [{
      "id": "62826bf03710200044e0bfc8bcbe5df1",
      "title": "Abel Tuter"
    }],
    "id": "63e8479fdb161300bde15901cf96191c",
    "title": "Abdul Waheed"
  },

  {
    "subs": [{
      "subs": [{
        "id": "12826bf03710200044e0bfc8bcbe5db1",
        "title": "Alfonso Griglen"
      }, {
        "subs": [{
          "id": "06826bf03710200044e0bfc8bcbe5d8a",
          "title": "Allyson Gillispie"
        }, {
          "id": "b282abf03710200044e0bfc8bcbe5d28",
          "title": "Allan Schwantd"
        }],
        "id": "22826bf03710200044e0bfc8bcbe5dec",
        "title": "Alejandra Prenatt"
      }],
      "id": "0a826bf03710200044e0bfc8bcbe5d7a",
      "title": "Adela Cervantsz"
    }, {
      "id": "4847c4d4d773020058c92cf65e61038e",
      "title": "Alisa Chinoy"
    }, {
      "id": "71826bf03710200044e0bfc8bcbe5d3b",
      "title": "Aileen Mottern "
    }, {
      "id": "a8f98bb0eb32010045e1a5115206fe3a",
      "title": "Abraham Lincoln"
    }],
    "id": "7c2e6109dbd65300bde15901cf9619b5",
    "title": "Raju Koyagura"
  }

];

let newArray = [];

function getAllId(arr, key) {
  arr.forEach(function(item) {
    for (let keys in item) {
      if (keys === key) {
        newArray.push(item[key])
      } else if (Array.isArray(item[keys])) {
        getAllId(item[keys], key)
      }
    }

  })
}
getAllId(data, 'id')
console.log(newArray)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接