Assuming I have JSON data response as follows below and a Search Input on my lightning component.
I want to search for a key or value and get a JS array of results.
For example :
Searching for "firstName" will bring results :
[{"firstName": "Tom"},{"firstName": "Maria"},{"firstName": "James"}]
Searching for "2" will bring results :
[{ "id": "2"}]
Data response :
{
"employees": {
"employee": [
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"photo": "https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg"
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"photo": "https://pbs.twimg.com/profile_images/3424509849/bfa1b9121afc39d1dcdb53cfc423bf12.jpeg"
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"photo": "https://pbs.twimg.com/profile_images/664886718559076352/M00cOLrh.jpg"
}
]
}
}
Been trying few approaches - but couldn't get a recursion work for all nested elements. This will work only on the first level keys :
const givenPath = component.get('v.searchInput');
for(let path in data) {
if(data.hasOwnProperty(givenPath)) {
newData = {
[givenPath] : data[givenPath]
}
}
}