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

时间:2018-09-17 07:54:21

标签: javascript arrays for-loop multidimensional-array

我有一个这样的对象:

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

我不知道如何实现。?

2 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:2)

创建一个递归函数,并通过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)