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

javascript find by value deep in a nested object/array(javascript在嵌套对象/数组中按值查找)
本文介绍了javascript在嵌套对象/数组中按值查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在函数中返回对象时遇到问题,假设我有一个对象:

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": {}
}];

我想要的是一个可以找到具有特定键和值的对象的函数(在纯 javascript 中),并且我创建了一个函数,但它只是不能正常工作?,我的功能:

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.

推荐答案

您在进行递归调用后缺少返回.如果在递归后找到对象,您需要继续将该结果冒泡(通过返回它).您还应该使用 i <len(不是 i <= len)正如@scott-marcus 指出的那样.

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.

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

这篇关于javascript在嵌套对象/数组中按值查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)
4