Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查对象中是否已存在对象_Javascript - Fatal编程技术网

Javascript 检查对象中是否已存在对象

Javascript 检查对象中是否已存在对象,javascript,Javascript,我想通过只拥有一个对象来检查给定对象中是否已经存在一个对象。 例如: const information = { ... city: { Streetname: '' } } 现在,我得到城市对象,并想检查它是否已经在信息对象中(不知道属性名称)。城市可以深入到信息对象中。 const contains=(item,data)=>item==data | | Object.getOwnPropertyNames(data).some(prop=>cont

我想通过只拥有一个对象来检查给定对象中是否已经存在一个对象。 例如:

const information = {
    ...
    city: {
        Streetname: ''
    }
}
现在,我得到城市对象,并想检查它是否已经在信息对象中(不知道属性名称)。城市可以深入到信息对象中。

const contains=(item,data)=>item==data | | Object.getOwnPropertyNames(data).some(prop=>contains(item,data[prop]);
常数信息={
城市:{
街道名称:“”
}
}
console.log(包含(information.city,information));

log(包含({},信息))
要获取对象的属性名,可以使用
对象.keys()
。第一个问题解决了。
现在我们需要遍历整个对象,包括嵌套对象。这是第二个问题。
并将其与查询对象进行比较。这是第三个问题

我假设我们有一个对象,它只包含“简单”的嵌套对象,具有原始值(我不考虑具有函数或数组的对象)

如果您使用
==
来比较
对象
,您将始终通过引用进行比较。在我们的例子中,我们有兴趣比较这些值。如果您想对更复杂的对象执行此操作(详细信息请阅读),则需要进行相当广泛的检查,我们将使用简化版本:

// Note that this only evaluates to true if EVERYTHING is equal.
// This includes the order of the properties, since we are eventually comparing strings here.
JSON.stringify(obj1) === JSON.stringify(obj2) 
在开始实现属性pathfinder之前,我将介绍一个简单的函数来检查给定值是对象还是原语值

function isObject(obj) {
  return obj === Object(obj); // if you pass a string it will create an object and compare it to a string and thus result to false
}
我们使用此函数来知道何时停止深入,因为我们达到了一个不包含任何其他对象的基本值。我们在整个对象中循环,并在每次找到嵌套对象时深入研究

function findPropertyPath(obj, currentPropertyPath) {
    const keys = isObject(obj) ? Object.keys(obj) : []; // if it is not an Object we want to assign an empty array or Object.keys() will implicitly cast a String to an array object  
    const previousPath = currentPropertyPath; // set to the parent node

    keys.forEach(key => {
        const currentObj = obj[key];
        currentPropertyPath = `${previousPath}.${key}`;

        if (JSON.stringify(currentObj) === JSON.stringify(queryObject)) console.log(currentPropertyPath); // this is what we are looking for
        findPropertyPath(currentObj, currentPropertyPath); // since we are using recursion this is not suited for deeply nested objects
    })
}

findPropertyPath(information, "information"); // call the function with the root key
这将使用递归查找包含与查询对象相等(按值比较)的对象的所有“属性路径”

information.house.city.findMe
information.findMeToo

所以您想检查
信息
是否有一个名为
城市
的属性?不,我的输入是城市对象,我想检查它是否已经在信息对象中。因此,我的目标实际上是获取属性名称o,您想看看
信息
是否包含属性
{Streetname:'}
?也可能有
城市2
Streetname
属性。。。我只有
对象{Streetname:'}
好的,那么你想检查
{Streetname:'}
是否在
信息
对象中?酷,这就是我要找的。我不知道我可以将obj与将它们转换为JSON字符串进行比较。然而,这是一个昂贵的操作。谢谢,阿尔伯特比较任意内容/深度的对象总是一项代价高昂的操作。另外,因为我不希望您的对象足够大,以至于您会遇到任何性能问题,因为您只对对象的一部分进行了字符串化。递归可能是一个更大的问题。JSON.stringify({prop1:1,prop2:2})!=JSON.stringify({prop2:2,prop1:1}),通过stringify进行比较不会给出可靠的结果。是的,请阅读我的代码中的注释://注意,只有当所有内容都相等时,这才计算为true。//这包括属性的顺序,因为我们最终将在这里比较字符串。
information.house.city.findMe
information.findMeToo