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]

closed as off-topic by glls, Jayant Das, David Reed, Pranay Jaiswal, battery.cord Dec 6 '18 at 18:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – glls, Jayant Das, David Reed, Pranay Jaiswal, battery.cord
If this question can be reworded to fit the rules in the help center, please edit the question.

  • this seems more in scope with stackoverflow rather than SFSE imo, i would suggest you browse that site since this is a frequent questions and may already have an answer, or you can post a new question there. – glls Dec 6 '18 at 15:41
  • I agree but since we have lightning JS this is a very fine line between the 2. the intention here is how to perform the recursive function in Lightning JS. – sfdx bomb Dec 7 '18 at 11:02

I wrote an article on a similar topic, i had to find all the attributes "attrName" in a JSON, maybe you can get ideas. The article is here

I wrote a function like:

//JavaScript Lightning (Salesforce)
removeAttributes : function(obj){
   if(!obj instanceof Array && !obj instanceof Object) {
   for(var i in obj) {
       if(obj instanceof Array || (obj instanceof Object && obj.hasOwnProperty(i))) {
           if(i == 'attributes') {
               //delete obj.attributes;

The code represents a recursive function, which accepts an object as a parameter (JSON is a JavaScript object) and searches for the various properties by checking all the Arrays and properties of the objects.

for each properties it is checked if it is an Array, an Object or if it is a parameter you are looking for ---> if (i = 'attributes')

I hope I have been helpful

  • you probably want to explain what it is your code does - as is, this would be considered dumping code as an answer which is normally frowned upon. – glls Dec 6 '18 at 17:50
  • Thats helpful thanks! I've add a path variable as extra input which replaced the 'attributes' and placed this recursive function in the helper.js file => Works great ! – sfdx bomb Dec 7 '18 at 11:00
  • Good! I'm happy to have been helpful – Andrea P. Dec 7 '18 at 16:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.