I have sample JSON like this
`{
values:[{
"name": "Base Url",
"url": "https://kubemanagement-prod.kohls.com"
},
{
"name": "Base Url newwww",
"url": "https://batman.com"
}]
}`
Currently when I add this paticular JSON to the lodash _.keys
gives me the result ["0", "1"]
which is basically the index of first and second object 0
and 1
.
What I exactly want is to retrieve all the keys of the JSON object including sub object properties as well. In this case ["values","0", "1","name","url"]
Does anyone knows a lodash method or a mechanism to retrieve all the keys given in complex JSON object to nth level?
language : Angular + Typescript
This function recursively gets the keys from an objects tree, using _.keys()
and _.flatMap()
and _.union()
to combine lists of keys, and get only the unique values:
const getAllKeys = obj => _.union( _.keys(obj), _.flatMap(obj, o => _.isObject(o) ? getAllKeys(o) : []) ) const arr = {"values": [{"name":"Base Url","url":"https://kubemanagement-prod.kohls.com"},{"name":"Base Url newwww","url":"https://batman.com"}]} const result = getAllKeys(arr) console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the same idea without lodash, using Object.values()
and Array.flatMap()
to iterate the current object (or array), and Array.concat()
and a Set to make the keys unique:
const getAllKeys = obj => [...new Set([].concat( Object.keys(obj), Object.values(obj).flatMap(o => typeof o === 'object' ? getAllKeys(o) : []) ))] const arr = {"values": [{"name":"Base Url","url":"https://kubemanagement-prod.kohls.com"},{"name":"Base Url newwww","url":"https://batman.com"}]} const result = getAllKeys(arr) console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Without array indexes:
const getAllKeys = obj => _.union( _.isArray(obj) ? [] : _.keys(obj), _.flatMap(obj, o => _.isObject(o) ? getAllKeys(o) : []) ) const arr = {"values": [{"name":"Base Url","url":"https://kubemanagement-prod.kohls.com"},{"name":"Base Url newwww","url":"https://batman.com"}]} const result = getAllKeys(arr) console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
You don't need lodash for this, Object.keys
is sufficient. You just write a recursive function, writing to a Set
, perhaps converting to array when you're done:
const array = [ { "name": "Base Url", "url": "https://kubemanagement-prod.kohls.com" }, { "name": "Base Url newwww", "url": "https://batman.com" } ]; function addAll(set, entries) { for (const entry of entries) { set.add(entry); } return set; } function allKeys(obj/*: object|array*/, keys/*: Set<string>*/ = new Set()) { addAll(keys, Object.keys(obj)); for (const entry of Object.values(obj)) { if (entry && typeof entry === "object") { allKeys(entry, keys); } } return keys; } console.log([...allKeys(array)]);
Or using the structure in your edit:
const array = { values:[{ "name": "Base Url", "url": "https://kubemanagement-prod.kohls.com" }, { "name": "Base Url newwww", "url": "https://batman.com" }] } function addAll(set, entries) { for (const entry of entries) { set.add(entry); } return set; } function allKeys(obj/*: object|array*/, keys/*: Set<string>*/ = new Set()) { addAll(keys, Object.keys(obj)); for (const entry of Object.values(obj)) { if (entry && typeof entry === "object") { allKeys(entry, keys); } } return keys; } console.log([...allKeys(array)]);
How about
const arr = {"values": [{"name":"Base Url","url":"https://kubemanagement-prod.kohls.com"},{"name":"Base Url newwww","url":"https://batman.com"}]} let result1 = Object.keys(arr) // get the object keys which is "values" let result2 = Object.keys(arr[result1]); // get the keys of the object name "0,1" here let result3 = Object.keys(arr[result1][0]); // get property names of one object "name and url" here let resutltant = result1.concat(result2, result3) // "merge array" console.log(resutltant)
Assuming that your object properties names will remain constant
If you use typescript why don't use Object.entries or Object.values to do that?
You need to add to tsconfig the next line:
lib: [ ...... "es2018", ]
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.