3

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.?

1
  • why not try to take the ìds from the first level first and look how this is done by simply iterating the array? Commented Sep 17, 2018 at 7:59

2 Answers 2

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;
});
Sign up to request clarification or add additional context in comments.

2 Comments

I was going to suggest to stringify and search for regex, but your answer (if it really works) is even better sollution
Simple and powerful alternative to writing custom recursion.
3

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.