繁体   English   中英

从多层嵌套数组JavaScript获取所有键值

[英]Get all key values from multi level nested array JavaScript

我有一个这样的对象:

 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); 

我不知道如何实现它。

您可以使用JSON.stringify轻松地在树上行走:

const ids = [];
JSON.stringify(data, (key, value) => {
  if (key === 'id') ids.push(value);
  return value;
});

创建一个递归函数,并检查该对象是否具有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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM