0

我有一个这样的嵌套对象:

let obj = {
_id: {},
person: {
    $search: {
        lname: true
    },
    _id: {},
    fname: {},
    something:{
        $search: {
            fname: true
        },
    }
},
code: {},
$search: {
    mname: true
},
vnvEmpName: {}
}

我必须检索$search键中的所有键,即使对象包含多次出现的$search它也应该返回其中的所有键,即:

lname 
fname
mname

我试过这个:

function search(obj, id) {
var result = "";
// iterate the object using for..in

    for (var keys in obj) {
        // check if the object has any property by that name
        if (obj.hasOwnProperty(keys) && typeof obj[keys] === 'object') {
            // if the key is not undefined get it's value
            if (obj[keys][id] !== undefined) {
                result = (obj[keys][id])
            } else {
                // else again call the same function using the new obj value
                console.log("reahced")
                search(obj[keys], id)
            }
        }

    }
    return result;

}
console.log(search(obj, '$search'))

但我只得到$search的第一个实例下的密钥( lname ) 。请帮我迭代它直到最后。

4

3 回答 3

0

您可以使用生成器创建迭代器函数,然后将其用于您的目的。这是一个示例代码:

let obj = { _id: {}, person: { $search: { lname: true }, _id: {}, fname: {}, something: { $search: { fname: true } } }, code: {}, $search: { mname: true }, vnvEmpName: {} };
//iterator function 
function* iterate(obj, stack='') {
    for (let property in obj) {
        if (obj.hasOwnProperty(property)) {
            if (typeof obj[property] == "object") {
                yield *iterate(obj[property], stack + '.' + property);
            } else {
                yield [stack, property, obj[property]];
            }
        }
    }
}
//using iterator function for searching
let searchkey = "$search"
for (let value of iterate(obj)) { 
    if(value[0].indexOf(searchkey) !== -1)
        console.log(value); 
}

我希望它有所帮助。

于 2018-05-17T11:45:57.077 回答
0

您可以检查键是否为$search并获取该嵌套键或检查嵌套对象。

function getSearch(object, key) {
    return Object.entries(object).reduce((r, [k, v]) => r.concat(
        k === key
            ? Object.keys(v)[0]
            : v && typeof v === 'object'
                ? getSearch(v, key)
                : []
    ), []);
}

var object = { _id: {}, person: { $search: { lname: true }, _id: {}, fname: {}, something: { $search: { fname: true } } }, code: {}, $search: { mname: true }, vnvEmpName: {} };

console.log(getSearch(object, '$search'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

于 2018-05-17T11:30:04.353 回答
0

You can try this

let obj = {
_id: {},
person: {
    $search : {
        lname: true
    },
    _id: {},
    fname: 'test',
    something:{
        $search: {
            fname: true
        },
    }
},
code: {},
$search: {
    mname: true
},
vnvEmpName: {}
}
var result;
function search(obj, id) {

// iterate the object using for..in
     for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
           if (obj[key][id] !== undefined) {
                result = (obj[key][id]);
                 return result;
            } else {
                // else again call the same function using the new obj value
                console.log("reahced")
                search(obj[key], id)
            }
        }
    }
    return result;
}
console.log(search(obj, 'lname'))

于 2018-05-17T11:54:16.297 回答