admin管理员组文章数量:1202968
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.
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.
2 Answers
Reset to default 15You'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
Just for future readers. Additionally to @user94559 answer.
- A more generic way would be to iterate over the keys instead of the indices, otherwise not all nested properties will be found.
- And check
obj[k]
before making the recursion call to ignore null values
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
本文标签: functionjavascript find by value deep in a nested objectarrayStack Overflow
版权声明:本文标题:function - javascript find by value deep in a nested objectarray - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738652827a2104971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论