繁体   English   中英

通过属性键深Javascript查找对象

[英]Find object by property key deep Javascript

我有以下代码:

您可以在此处转到jsbin。.https://jsbin.com/wupukuhita/1/edit ? js,控制台

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闭包,它基本上是另一个函数中的一个函数,第二个函数可以访问主要函数对象
在此处查看完整代码,除了返回数组之外,它的工作原理与您相同

 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]); 

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

 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; } 

 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(); 

暂无
暂无

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

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