我想知道我应该使用哪种逻辑来检查包含在祖父对象中的每个对象的父对象数组
大家好,我想检查这个值是否例如:“127.0.0.1”存在于这个对象中(MyObject 中有 2k 个对象)
{
"name" : MyObject
"value": [
{
"name" : "Object1",
"properties":{
"address" : [
"13.65.25.19/32",
"13.66.60.119/32",
]
}
},
{
"name" : "Object2",
"properties":{
"address" : [
"13.65.25.19/32",
"127.0.0.1",
]
}
}
]
}
顺便说一句,include() 是否需要匹配整个字符串,或者例如,如果 127.0.0.1 在我的对象 127.0.0.1/32 中是这样的,即使有一个 IP 范围,我仍然可以检索它?
您的数据结构非常具体,因此您可以编写一个可以反复调用的自定义方法。它会检查一个
const obj = {
name: 'MyObject',
value: [
{
name: 'Object1',
properties: {
address: ['13.65.25.19/32', '13.66.60.119/32'],
},
},
{
name: 'Object2',
properties: {
address: ['13.65.25.19/32', '127.0.0.1'],
},
},
],
};
const address = '127.0.0.1';
const includesAddress = (address) => {
for (const val of obj.value) {
if (val.properties.address.some((a) => address === a)) return true;
}
return false;
};
console.log(includesAddress(address));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句