Reputation: 181
I'm trying to transform an object that could have varying levels of nesting:
const obj = {
some: {
thing: {
good: 'yo'
},
one: 'else'
},
something: {
bad: 'oy'
},
somethingtasty: 'mmm'
}
into an array of objects containing the original path of the value and the value:
const arr = [{
path: 'some.thing.good',
value: 'yo'
}, {
path: 'some.one',
value: 'else
}, {
path: 'something.bad',
value: 'oy'
}, {
path: 'somethingtasty',
value: 'mmm'
}]
I found a helpful answer on SO for a similar question dealing with objects of varying nestedness:
https://stackoverflow.com/a/2631198
But this doesn't solve
I also tried looking to see if lodash had a method (or methods) that could help like:
https://github.com/node4good/lodash-contrib/blob/master/docs/_.object.selectors.js.md#getpath
or:
https://lodash.com/docs/4.17.11#flatMapDeep
But this doesn't help if I don't know the path to the values I need to get.
Is there a way in javascript to recurse through an object and save its keys and value in an array?
Upvotes: 2
Views: 621
Reputation: 386520
You could take an iterative and recursive approach.
function getPathes(object, temp = '') {
return Object
.entries(object)
.reduce(
(r, [key, value]) =>
(path => r.concat(value && typeof value === 'object'
? getPathes(value, path)
: { path, value }))
(temp + (temp && '.') + key),
[]
);
}
const obj = { some: { thing: { good: 'yo' }, one: 'else' }, something: { bad: 'oy' }, somethingtasty: 'mmm' };
console.log(getPathes(obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 10870
const obj = {
some: {
thing: {
good: 'yo'
},
one: 'else'
},
something: {
bad: 'oy'
},
somethingtasty: 'mmm'
}
const deepLinkFinder = (obj) => {
let arr = []
const getKeyPath = (obj, path) => {
for (const key of Object.keys(obj)) {
if (typeof obj[key] === 'object')
getKeyPath(obj[key], path ?
`${path}.${key}` :
`${key}`)
else
arr.push({
path: path ?
`${path}.${key}` :
`${key}`,
value: obj[key]
})
}
}
getKeyPath(obj)
return arr
}
console.log(deepLinkFinder(obj))
Upvotes: 1
Reputation: 122027
You could do this using reduce
method to create recursive function.
const obj = {"some":{"thing":{"good":"yo"},"one":"else"},"something":{"bad":"oy"},"somethingtasty":"mmm"}
function paths(obj, prev = "") {
return Object.keys(obj).reduce((r, e) => {
const path = prev + (prev ? '.' + e : e);
const value = obj[e];
if (typeof value == 'object') {
r.push(...paths(value, path))
} else {
r.push({path,value})
}
return r
}, [])
}
const result = paths(obj)
console.log(result)
Upvotes: 2
Reputation: 370639
One possible approach is to recursively iterate over the Object.entries
:
const obj = {
some: {
thing: {
good: 'yo'
},
one: 'else'
},
something: {
bad: 'oy'
},
somethingtasty: 'mmm'
};
const getNested = (obj, propArr) => {
const [prop, value] = Object.entries(obj)[0];
propArr.push(prop);
if (typeof value === 'object') {
return getNested(value, propArr);
}
return { path: propArr.join('.'), value };
};
const arr = Object.entries(obj)
.map(([top, value]) =>
typeof value === 'object' ? getNested(obj, [top]) : ({ path: top, value })
);
console.log(arr);
Upvotes: 0