Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript find by value deep in a nested object/array

hello , I have a problem returning an object in my function, Let's say I have an object:

var elements = [{
    "fields": null,
    "id_base": "nv_container",
    "icon": "layout",
    "name": "container",
    "is_container": true,
    "elements" : [
        //another elements set here
    ]
}, 
{
    "id_base": "novo_example_elementsec",
    "name": "hello",
    "icon": "edit",
    "view": {}
}];

what i want is a function (in pure javascript) that can find an object with a specific key and value , and i have created a function but its just not working fine ? , my function :

function findNested(obj, key, value) {
    //Early return
    if (obj[key] === value) {
        console.log( 'before return' ); //until here . its fine
        return obj; //not working
    } else {
        for (var i = 0, len = Object.keys(obj).length; i <= len; i++) {
            if (typeof obj[i] == 'object') {
                this.findNested(obj[i] , key, value);
            }
        }
    }
}

I just can't see what I've done wrong ?

thanks.

like image 954
Omar Badran Avatar asked Jul 26 '17 19:07

Omar Badran


People also ask

How do you find the object property by key deep in a nested JavaScript array?

To use: let result = deepSearchByKey(arrayOrObject, 'key', 'value'); This will return the object containing the matching key and value.

How do I find the value of a nested object?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };

How do I access an inner array?

To access the elements of the inner arrays, you simply use two sets of square brackets. For example, pets[1][2] accesses the 3rd element of the array inside the 2nd element of the pets array.


1 Answers

You're missing a return after making the recursive call. If the object is found after recursing, you need to continue to bubble that result up (by returning it). You should also be using i < len (not i <= len) as pointed out by @scott-marcus.

var elements = [{
    "fields": null,
    "id_base": "nv_container",
    "icon": "layout",
    "name": "container",
    "is_container": true,
    "elements": [
      //another elements set here
    ]
  },
  {
    "id_base": "novo_example_elementsec",
    "name": "hello",
    "icon": "edit",
    "view": {}
  }
];

function findNested(obj, key, value) {
  // Base case
  if (obj[key] === value) {
    return obj;
  } else {
    for (var i = 0, len = Object.keys(obj).length; i < len; i++) {
      if (typeof obj[i] == 'object') {
        var found = this.findNested(obj[i], key, value);
        if (found) {
          // If the object was found in the recursive call, bubble it up.
          return found;
        }
      }
    }
  }
}

console.log(findNested(elements, "icon", "layout")); // returns object
console.log(findNested(elements, "icon", "edit")); // returns object
console.log(findNested(elements, "foo", "bar")); // returns undefined
like image 155
user94559 Avatar answered Sep 30 '22 16:09

user94559