简体   繁体   English

javascript按值在嵌套对象/数组深处查找

[英]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 ?我想要的是一个函数(在纯 javascript 中),它可以找到一个具有特定键和值的对象,我已经创建了一个函数,但它不能正常工作? , 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.谢谢。

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. 您还应该使用@ scott-marcus指出的i < len (不是i <= len )。

 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 

Just for future readers.只为未来的读者。 Additionally to @user94559 answer.除了@user94559答案。

  1. A more generic way would be to iterate over the keys instead of the indices , otherwise not all nested properties will found.更通用的方法是迭代键而不是索引,否则不会找到所有嵌套的属性。
  2. And check obj[k] before making the recursion call to ignore null values并在进行递归调用以忽略空值之前检查obj[k]

 var elements = [{ "fields": null, "id_base": "nv_container", "icon": "layout", "name": "container", "is_container": true, "elements": [ {"icon": "nested"} ] }, { "id_base": "novo_example_elementsec", "name": "hello", "icon": "edit", "view": {} } ]; function findNested(obj, key, value) { // Base case if (obj[key] === value) { return obj; } else { var keys = Object.keys(obj); // add this line to iterate over the keys for (var i = 0, len = keys.length; i < len; i++) { var k = keys[i]; // use this key for iteration, instead of index "i" // add "obj[k] &&" to ignore null values if (obj[k] && typeof obj[k] == 'object') { var found = findNested(obj[k], 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 // this will work now console.log(findNested(elements, "icon", "nested")); // returns object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM