Steve Bennett
Steve Bennett

Reputation: 126777

Get array of all Lodash paths of object

I'm trying to figure out if there is a simple way to get a list of all the "paths" (as defined by Lodash) of an object.

That is:

getPaths({ a: 3, b: { c: 4, d: 0 }})

=> 
[
  ['a'],
  ['b', 'c'],
  ['b', 'd']
]

Is there such a mechanism in Lodash? Or a simple way to create one?

Upvotes: 1

Views: 2908

Answers (5)

Pranav C Balan
Pranav C Balan

Reputation: 115272

You can use a recursive approach as follows.

const getPaths = (obj, arr = [], res = []) => {
  Object.entries(obj).forEach(([key, value]) => {
    if (typeof value === 'object' && value) getPaths(value, [...arr, key], res)
    else res.push([...arr, key])
  });
  return res;
}


console.log(getPaths({ a: 3, b: { c: 4, d: 0 }}));

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386868

You could get the pathes without storing the visited path to the actual object by reducing the given nested path or taking an empty placeholder [[]] for reducing just the actual key.

function getPaths(object) {
    return object && typeof object === 'object' && Object.keys(object).reduce((p, k) => 
        (getPaths(object[k]) || [[]]).reduce((r, a) =>
            [...r, [k, ...a]], p), []);
}

console.log(getPaths({ a: 3, b: { c: 4, d: 0 } }));

Upvotes: 1

Steve Bennett
Steve Bennett

Reputation: 126777

Ok, if there isn't an included method, here is my answer:

function getPaths(obj, prefix = []) {
  return Object.keys(obj).reduce((arr, key) => {
    const path = [...prefix, key];
    const content = typeof obj[key] === 'object' ? getPaths(obj[key], path) : [path];
    return [...arr, ...content];
  }, []);
}
       
console.log(getPaths({ a: 3, b: { c: 4, d: 0, e: { f: 9} }}));
;

Upvotes: 0

Ori Drori
Ori Drori

Reputation: 193120

There isn't an out of the box method, but you can use _.transform() recursively to get the paths:

const getPaths = o =>
  _.transform(o, (acc, v, k) => {

    const keys = _.isObject(v) && !_.isEmpty(v) ? 
      _.map(getPaths(v), sk => _.concat(k, ...sk))
      :
      [[k]];
  
    acc.push(...keys);
  }, []);


const result = getPaths({ a: 3, b: { c: 4, d: 0 }, e: {}, f: [{ g: {h: 5}}] });

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Upvotes: 1

Jack Bashford
Jack Bashford

Reputation: 44145

Recursive function:

function getPaths(obj, prefix) {
  let result = Object.keys(obj).map(key => {
    if (typeof obj[key] == "object" && obj[key] != null) {
      return getPaths(obj[key], key);
    } else {
      return prefix ? [prefix, key] : key;
    }
  });
  return result;
}

console.log(getPaths({ a: 3, b: { c: 4, d: 0 }}));

Upvotes: 0

Related Questions