按属性键深度Javascript查找对象

问题描述 投票:0回答:3

我有以下代码:

你可以去这里的jsbin .. https://jsbin.com/wupukuhita/1/edit?js,console

var foundObjects = [];
var findObjectByLabel = function(obj, key) {

    var foundObject = [];    
    for(var i in obj) {
      if(typeof obj[i] === "object"){
        if(typeof obj[i][key] !== "undefined"){
            foundObjects.push(obj[i]);
        }
        findObjectByLabel(obj[i],key);
      }
    }
    return null;
};

我在一个对象上递归迭代,以找出是否存在某个属性。然后,如果它确实存在,则返回父对象。

您可以查看jsbin链接以获取完整示例。

我不喜欢函数之外的foundObjects。

我如何将它放在函数中,只是从函数返回包含某个属性的对象。

https://jsbin.com/wupukuhita/1/edit?js,console

javascript recursion
3个回答
1
投票

您可以使用javascript闭包,它基本上是另一个函数内部的函数,第二个函数可以访问主函数对象 看到这里的完整代码,除了我们返回数组之外,它的工作原理与你的相同

var foundObjects = function (obj,key) {
    var foundObject = [];
    var findObjectByLabel = function(obj,key) {
        for (var i in obj) {
            if (typeof obj[i] === 'object') {
                if (typeof obj[i][key] !== 'undefined') {
                    foundObject.push(obj[i]);
                }
                findObjectByLabel(obj[i], key);
            }
        }
        return null;
    };
    findObjectByLabel(obj,key);
    return foundObject ;
}


var mainObj = {
    name: 'MainForm', // main form
    type: 'Form',
    dirty: false,
    valid: true,
    Errors: [],
    formOrInputElements: [
        {
            name: 'Age', // normal input
            type: 'Text',
            value: '',
            dirty: true,
            valid1: true,
            test: {
                name: 'test',
                valid1: false,
            },
            Errors: [],
        },
        {
            name: 'CNP', // normal input
            type: 'Text',
            value: '',
            dirty: true,
            valid: true,
            Errors: [],
        },
    ],
};

let foundObject = foundObjects(mainObj, 'valid1');

console.log(foundObject[0]);
console.log(foundObject[1]);

0
投票

或者,您可以使用array#reduce并遍历对象中的每个键值。如果是数组,则递归调用每个对象的函数。如果是对象,则使用该对象调用该函数。

var mainObj = { name: "MainForm", type: "Form", dirty: false, valid: true, Errors: [], formOrInputElements: [ { name: "Age", type: "Text", value: "", dirty: true, valid1: true, test: { name: "test", valid1: false }, Errors: [] }, { name: "CNP", type:"Text", value: "", dirty: true, valid: true, Errors: [] } ] }

var findObjectByLabel = function(obj, key) {

  return Object.keys(obj).reduce((r, k) => {
    if (k === key) {
      r.push(Object.assign({}, obj));
    } else if (Array.isArray(obj[k])) {
      obj[k].forEach(x => r = r.concat(findObjectByLabel(x, key)));
    } else if (typeof obj[k] === 'object') {
      r = r.concat(findObjectByLabel(obj[k], key));
    }
    return r;
  }, []);

};

console.log(findObjectByLabel(mainObj, "valid1"));
.as-console-wrapper { max-height: 100% !important; top: 0; }

0
投票

function findObjectByLabel(haystack, needle, buffer = []) {
    if (typeof haystack === 'object') {
        for (const prop in haystack) {
            const result = prop === needle ? [haystack] : findObjectByLabel(haystack[prop], needle);
            if (result.length > 0) {
                buffer = buffer.concat(result);
            }
        }
    }
    return buffer;
}


// Unit test
function test_findObjectByLabel() {
    const obj = {
        foo: {
            foo1: {
                item1: 'item1',
                item2: 'item2',
                item3: 'item3',
            },
            foo2: {
                item1: 'item1',
                item2: 'item2',
                item3: 'item3',
                subFoo: {
                    item1: 'item1',
                    item2: 'item2',
                    needle: 'needle',
                }
            }
        },
        bar: {
            bar1: {
                item1: 'item1',
                item2: 'item2',
                item3: 'item3',
            },
            bar2: {
                item1: 'item1',
                item2: 'item2',
                item3: 'item3',
                needle: 'needle',
            }
        },
    }

    const expected = [
        obj.foo.foo2.subFoo, // <-- contain "needle"
        obj.bar.bar2,        // <-- contain "needle"
    ];
    const actual = findObjectByLabel(obj, 'needle');

    if (JSON.stringify(actual) === JSON.stringify(expected)) {
        console.log('Pass');
        console.log('expected => ', JSON.stringify(expected, null, 4));
        console.log('actual   => ', JSON.stringify(actual, null, 4));
    } else {
        console.log('Fail');

        console.log('Actual')
        console.log(JSON.stringify(actual));
        console.log('is not equal to expected');
        console.log(JSON.stringify(expected));
    }
}

test_findObjectByLabel();
© www.soinside.com 2019 - 2024. All rights reserved.