最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get all key values from multi level nested array JavaScript - Stack Overflow

programmeradmin5浏览0评论

I have an object like this:

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)

I have an object like this:

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)

Now I want to retrieve all the id values as a new array without consideration of which nested level it is.

My expected result is something like this::

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

console.log(result);

I am not getting any idea how to achieve it.?

Share Improve this question asked Sep 17, 2018 at 7:54 asifasif 2791 gold badge3 silver badges19 bronze badges 1
  • why not try to take the ìds from the first level first and look how this is done by simply iterating the array? – Nina Scholz Commented Sep 17, 2018 at 7:59
Add a comment  | 

2 Answers 2

Reset to default 20

You can use JSON.stringify to walk on the tree easily:

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

Create a recursive function and check if that object have a key by id. Push the value of id. If the key is another array then call the same function with new value

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)

发布评论

评论列表(0)

  1. 暂无评论