Javascript function to get specific keys and their values from (optionally nested) objects.
This function will return an array of objects containing the specified keys and values, but only if all of the keys have values.
param | type | detail |
---|---|---|
obj |
object | Object that we want to search through. May be nested. |
keys |
array | Array of keys to return. ** |
recurse |
string | [optional] Sub-objects to look within. |
** All keys must have values if any of them are to be returned.
Say you have the following nested object (this one's for use with Bootstrap-TreeView) and you want to extract (say) all the ids
and their respective filePaths
- that's what getKeysVals is for:
const nodes = [
{
"id": 1526297177970,
"text": "Some top-level item",
"nodes": [
{
"id": 1526297185466,
"tags": [1],
"text": "text1",
"state": { "checked": true, "expanded": true },
"filePath": "path1.CSV"
},
{
"id": 1526297195199,
"tags": [1],
"text": "Some sub-node",
"nodes": [
{
"id": 1526297202132,
"tags": [1],
"text": "text2",
"state": { "checked": true, "expanded": true },
"filePath": "path2.CSV"
},
{
"id": 1526297209980,
"tags": [1],
"text": "text3",
"state": { "checked": true, "expanded": true },
"filePath": "path3.CSV"
}
],
"state": { "checked": true }
}
],
"state": { "checked": true }
}
];
Let's return those nodes which have values for id
, text
and filePath
.
getKeysVals(nodes[0].nodes, ['id', 'text', 'filePath'], 'nodes');
Result:
[
{ id: 1526297185466, text: "text1", filePath: "path1.CSV" },
{ id: 1526297202132, text: "text2", filePath: "path2.CSV" },
{ id: 1526297209980, text: "text3", filePath: "path3.CSV" }
]