简体   繁体   中英

Find all values by specific key in a deep nested object

How would I find all values by specific key in a deep nested object?

For example, if I have an object like this:

const myObj = {
  id: 1,
  children: [
    {
      id: 2,
      children: [
        {
          id: 3
        }
      ]
    },
    {
      id: 4,
      children: [
        {
          id: 5,
          children: [
            {
              id: 6,
              children: [
                {
                  id: 7,
                }
              ]
            }
          ]
        }
      ]
    },
  ]
}

How would I get an array of all values throughout all nests of this obj by the key of id .

Note: children is a consistent name, and id 's won't exist outside of a children object.

So from the obj, I would like to produce an array like this:

const idArray = [1, 2, 3, 4, 5, 6, 7]

This is a bit late but for anyone else finding this, here is a clean, generic recursive function:

function findAllByKey(obj, keyToFind) {
  return Object.entries(obj)
    .reduce((acc, [key, value]) => (key === keyToFind)
      ? acc.concat(value)
      : (typeof value === 'object')
      ? acc.concat(findAllByKey(value, keyToFind))
      : acc
    , [])
}

// USAGE
findAllByKey(myObj, 'id')

You could make a recursive function like this:

idArray = []

function func(obj) {
  idArray.push(obj.id)
  if (!obj.children) {
    return
  }

  obj.children.forEach(child => func(child))
}

Snippet for your sample:

 const myObj = { id: 1, children: [{ id: 2, children: [{ id: 3 }] }, { id: 4, children: [{ id: 5, children: [{ id: 6, children: [{ id: 7, }] }] }] }, ] } idArray = [] function func(obj) { idArray.push(obj.id) if (!obj.children) { return } obj.children.forEach(child => func(child)) } func(myObj) console.log(idArray)

You can make a generic recursive function that works with any property and any object.

This uses Object.entries() , Object.keys() , Array.reduce() , Array.isArray() , Array.map() and Array.flat() .

The stopping condition is when the object passed in is empty:

 const myObj = { id: 1, anyProp: [{ id: 2, thing: { a: 1, id: 10 }, children: [{ id: 3 }] }, { id: 4, children: [{ id: 5, children: [{ id: 6, children: [{ id: 7 }] }] }] }] }; const getValues = prop => obj => { if (!Object.keys(obj).length) { return []; } return Object.entries(obj).reduce((acc, [key, val]) => { if (key === prop) { acc.push(val); } else { acc.push(Array.isArray(val) ? val.map(getIds).flat() : getIds(val)); } return acc.flat(); }, []); } const getIds = getValues('id'); console.log(getIds(myObj));

Note: children is a consistent name, and id 's wont exist outside of a children object.

So from the obj, I would like to produce an array like this:

 const idArray = [1, 2, 3, 4, 5, 6, 7]

Given that the question does not contain any restrictions on how the output is derived from the input and that the input is consistent, where the value of property "id" is a digit and id property is defined only within "children" property, save for case of the first "id" in the object, the input JavaScript plain object can be converted to a JSON string using JSON.stringify() , RegExp /"id":\\d+/g matches the "id" property and one or more digit characters following the property name, which is then mapped to .match() the digit portion of the previous match using Regexp \\d+ and convert the array value to a JavaScript number using addition operator +

 const myObject = {"id":1,"children":[{"id":2,"children":[{"id":3}]},{"id":4,"children":[{"id":5,"children":[{"id":6,"children":[{"id":7}]}]}]}]}; let res = JSON.stringify(myObject).match(/"id":\\d+/g).map(m => +m.match(/\\d+/)); console.log(res);

JSON.stringify() replacer function can alternatively be used to .push() the value of every "id" property name within the object to an array

 const myObject = {"id":1,"children":[{"id":2,"children":[{"id":3}]},{"id":4,"children":[{"id":5,"children":[{"id":6,"children":[{"id":7}]}]}]}]}; const getPropValues = (o, prop) => (res => (JSON.stringify(o, (key, value) => (key === prop && res.push(value), value)), res))([]); let res = getPropValues(myObject, "id"); console.log(res);

Since the property values of the input to be matched are digits, all the JavaScript object can be converted to a string and RegExp \\D can be used to replace all characters that are not digits, spread resulting string to array, and .map() digits to JavaScript numbers

let res = [...JSON.stringify(myObj).replace(/\D/g,"")].map(Number)

Using recursion.

 const myObj = { id: 1, children: [ { id: 2, children: [ { id: 3 } ] }, { id: 4, children: [ { id: 5, children: [ { id: 6, children: [ { id: 7, } ] } ] } ] }, ]}, loop = (array, key, obj) => { if (!obj.children) return; obj.children.forEach(c => { if (c[key]) array.push(c[key]); // is not present, skip! loop(array, key, c); }); }, arr = myObj["id"] ? [myObj["id"]] : []; loop(arr, "id", myObj); console.log(arr);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

You can make a recursive function with Object.entries like so:

 const myObj = { id: 1, children: [{ id: 2, children: [{ id: 3 }] }, { id: 4, children: [{ id: 5, children: [{ id: 6, children: [{ id: 7, }] }] }] }, ] }; function findIds(obj) { const entries = Object.entries(obj); let result = entries.map(e => { if (e[0] == "children") { return e[1].map(child => findIds(child)); } else { return e[1]; } }); function flatten(arr, flat = []) { for (let i = 0, length = arr.length; i < length; i++) { const value = arr[i]; if (Array.isArray(value)) { flatten(value, flat); } else { flat.push(value); } } return flat; } return flatten(result); } var ids = findIds(myObj); console.log(ids);

Flattening function from this answer

ES5 syntax:

 var myObj = { id: 1, children: [{ id: 2, children: [{ id: 3 }] }, { id: 4, children: [{ id: 5, children: [{ id: 6, children: [{ id: 7, }] }] }] }, ] }; function findIds(obj) { const entries = Object.entries(obj); let result = entries.map(function(e) { if (e[0] == "children") { return e[1].map(function(child) { return findIds(child) }); } else { return e[1]; } }); function flatten(arr, flat = []) { for (let i = 0, length = arr.length; i < length; i++) { const value = arr[i]; if (Array.isArray(value)) { flatten(value, flat); } else { flat.push(value); } } return flat; } return flatten(result); } var ids = findIds(myObj); console.log(ids);

I found steve's answer to be most suited for my needs in extrapolating this out and creating a general recursive function. That said, I encountered issues when dealing with nulls and undefined values, so I extended the condition to accommodate for this. This approach uses:

Array.reduce() - It uses an accumulator function which appends the value's onto the result array. It also splits each object into it's key:value pair which allows you to take the following steps:

  1. Have you've found the key? If so, add it to the array;
  2. If not, have I found an object with values? If so, the key is possibly within there. Keep digging by calling the function on this object and append the result onto the result array; and
  3. Finally, if this is not an object, return the result array unchanged.

Hope it helps!

 const myObj = { id: 1, children: [{ id: 2, children: [{ id: 3 }] }, { id: 4, children: [{ id: 5, children: [{ id: 6, children: [{ id: 7, }] }] }] }, ] } function findAllByKey(obj, keyToFind) { return Object.entries(obj) .reduce((acc, [key, value]) => (key === keyToFind) ? acc.concat(value) : (typeof value === 'object' && value) ? acc.concat(findAllByKey(value, keyToFind)) : acc , []) || []; } const ids = findAllByKey(myObj, 'id'); console.log(ids)

We use object-scan for a lot of our data processing needs now. It makes the code much more maintainable, but does take a moment to wrap your head around. Here is how you could use it to answer your question

 // const objectScan = require('object-scan'); const find = (data, needle) => objectScan([needle], { rtn: 'value' })(data); const myObj = { id: 1, children: [{ id: 2, children: [ { id: 3 } ] }, { id: 4, children: [ { id: 5, children: [ { id: 6, children: [ { id: 7 } ] } ] } ] }] }; console.log(find(myObj, '**.id')); // => [ 7, 6, 5, 4, 3, 2, 1 ]
 .as-console-wrapper {max-height: 100% !important; top: 0}
 <script src="https://bundle.run/object-scan@13.7.1"></script>

Disclaimer : I'm the author of object-scan

import {flattenDeep} from 'lodash';

/**
 * Extracts all values from an object (also nested objects)
 * into a single array
 *
 * @param obj
 * @returns
 *
 * @example
 * const test = {
 *  alpha: 'foo',
 *  beta: {
 *    gamma: 'bar',
 *    lambda: 'baz'
 *  }
 * }
 *
 * objectFlatten(test) // ['foo', 'bar', 'baz']
 */
export function objectFlatten(obj: {}) {
  const result = [];
  for (const prop in obj) {
    const value = obj[prop];
    if (typeof value === 'object') {
      result.push(objectFlatten(value));
    } else {
      result.push(value);
    }
  }
  return flattenDeep(result);
}
let str = JSON.stringify(myObj);
let array = str.match(/\d+/g).map(v => v * 1);
console.log(array); // [1, 2, 3, 4, 5, 6, 7]

Below solution is generic which will return all values by matching nested keys as well eg for below json object

{
   "a":1,
   "b":{
      "a":{
         "a":"red"
      }
   },
   "c":{
      "d":2
   }
}

to find all values matching key "a" output should be return

[1,{a:"red"},"red"]
const findkey = (obj, key) => {
  let arr = [];
  if (isPrimitive(obj)) return obj;

  for (let [k, val] of Object.entries(obj)) {
    if (k === key) arr.push(val);
    if (!isPrimitive(val)) arr = [...arr, ...findkey(val, key)];
  }
  return arr;
};

const isPrimitive = (val) => {
  return val !== Object(val);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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