简体   繁体   English

如何检查父 object 是否包含子对象属性? 有什么办法不将 object 转换为数组?

[英]How to check if parent object contains child objects properties? Is there any way without converting object to array?

I want to check if the object property is available to another object that returns true if it has.我想检查 object 属性是否可用于另一个 object 如果有则返回 true。 I tried this way, but it gives false.我试过这种方式,但它给出了错误。 any suggestions will be appreciated.任何建议将不胜感激。

const checked = {0: true, 1: true, 2: true, 3 true, 4: true}
const newChecked = {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true}

Object.entries(checked).every(e => Object.entries(newChecked).includes(e))

If you just want to check properties only, you can use Object.keys to do this如果您只想检查属性,可以使用Object.keys来执行此操作

 const checked = { 0: true, 1: true, 2: true, 3: true, 4: true }; const newChecked = { 0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, }; const has = Object.keys(checked).every((key) => Object.keys(newChecked).includes(key) ); console.log(has);

For both key and value check, you can combine the use of Object.entries and Array.prototype.findIndex对于键值检查,可以结合使用Object.entriesArray.prototype.findIndex

 const checked = { 0: true, 1: true, 2: true, 3: true, 4: true }; const newChecked = { 0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, }; const has = Object.entries(checked).every( ([keyToFind, valueToFind]) => Object.entries(newChecked).findIndex( ([key, value]) => key === keyToFind && value === valueToFind );== -1 ). console;log(has);

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

相关问题 如果 object 包含在 javascript 的对象数组中,如何检查整个原始 object(不使用任何属性)? - How to check with whole raw object (without using any property) if that object contains in array of objects in javascript? 将对象属性转换为对象数组 - Converting object properties to array of objects 如何检查包含另一个对象数组的对象数组是否具有属性 - How to check if array of objects that contains another array of object has property 如何将数组推送到仅匹配父属性的嵌套子对象? - How to push array to nested child object of only matching parent properties? 如何从数组中总结对象的属性并将其存储到父对象? - How to sum properties of objects from array and store it to parent object? 在JS中如何检查对象数组是否包含一个对象? - How in JS check if array of objects contains one object? 检查带有对象的数组是否包含JS中的对象 - Check if array with objects contains an object in JS 检查对象数组中的任何对象是否包含另一个对象中的所有键/值对 - Check if any object in an array of objects contains all of the key/value pairs in another object 如何检查对象包含一组属性 - How to check an object contains a set of properties 检查数组是否包含没有引用的对象 - Check if array contains object without reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM