javascript - javascript - better search value script in nested arrays
问题描述
I need to search for a value in nested array to answer the question :
Is there an emailAddress of "type":"CONTACT"
with "value":"email4@example.com"
?
I wrote the following basic algorithm (see fiddle) which is giving the expected result, but I guess there is a better and shorter way to solve it ...
const infos = [
{
"resourceName":"name1",
"id":"id1",
"emailAddresses":[
{ "metadata":{ "primary":true, "source":{ "type":"CONTACT", "id":"m1" } , "value":"email1@example.com"}},
{ "metadata":{ "primary":false, "source":{ "type":"CONTACT", "id":"m2" } , "value":"email2@example.com"}}
]
},
{
"resourceName":"name2",
"id":"id2",
"emailAddresses":[
{ "metadata":{ "primary":true, "source":{ "type":"FAMILY", "id":"m3" } , "value":"email3@example.com"}},
{ "metadata":{ "primary":false, "source":{ "type":"CONTACT", "id":"m4" } , "value":"email4@example.com"}},
{ "metadata":{ "primary":false, "source":{ "type":"BUSINESS", "id":"m5" } , "value":"email5@example.com"}}
]
},
{
"resourceName":"name3",
"id":"id3",
"emailAddresses":[
{ "metadata":{ "primary":true, "source":{ "type":"CONTACT", "id":"m5" } , "value":"email6@example.com"}},
{ "metadata":{ "primary":false, "source":{ "type":"FAMILY", "id":"m6" } , "value":"email7@example.com"}}
]
}
];
const search_email = "email4@example.com";
let parent_key = null;
infos.forEach( info => {
info.emailAddresses.forEach( ema => {
if ((ema.metadata.source.type === 'CONTACT') && (ema.metadata.value === search_email )) {
parent_key = info.id;
}
})
});
if ( parent_key === null) {
console.log('NOT FOUND');
} else {
console.log('FOUND - PARENT KEY: ', parent_key);
}
I can stop the search on the first found state... just need to know if the searched email does exist.
解决方案
You can use array#some
to iterate through infos
array, then again use array#some
to look inside emailAddresses
array where source
type
is CONTACT
and value
is the email address you provided.
The some() method tests whether at least one element in the array passes the test implemented by the provided function.
If an element is found, array#some()
immediately returns true.
const infos =[ { "resourceName":"name1", "id":"id1", "emailAddresses":[ { "metadata":{ "primary":true, "source":{ "type":"CONTACT", "id":"m1" } , "value":"email1@example.com"}}, { "metadata":{ "primary":false, "source":{ "type":"CONTACT", "id":"m2" }, "value":"email2@example.com"}} ] }, { "resourceName":"name2", "id":"id2", "emailAddresses":[ { "metadata":{ "primary":true, "source":{ "type":"FAMILY", "id":"m3" } , "value":"email3@example.com"}}, { "metadata":{ "primary":false, "source":{ "type":"CONTACT","id":"m4" } , "value":"email4@example.com"}}, { "metadata":{ "primary":false, "source":{ "type":"BUSINESS", "id":"m5" } , "value":"email5@example.com"}} ] }, { "resourceName":"name3", "id":"id3", "emailAddresses":[ { "metadata":{ "primary":true, "source":{"type":"CONTACT", "id":"m5" } , "value":"email6@example.com"}}, { "metadata":{ "primary":false, "source":{ "type":"FAMILY", "id":"m6" } , "value":"email7@example.com"}} ] } ],
searchEmail = "email4@example.com";
found = infos.some(({emailAddresses}) =>
emailAddresses.some(({metadata}) => metadata.source.type === 'CONTACT' && metadata.value === searchEmail)
);
console.log(found);
推荐阅读
- bash - 如何通过shell脚本更改当前目录并在那里执行命令?
- reference - 我可以使用“继承”使用引用将特定属性添加到带有 Schema.org 的 json-ld 中的节点吗?
- java - 将图像放在 Java 项目中的哪个位置以便在 index.html 中呈现
- c# - 为什么编译器选择字符串而不是隐式字符数组的扩展方法?
- ruby-on-rails - # 未定义的方法“last”
- php - HTML 内容如何作为附件响应返回以下载 pdf 和 docx
- python - TensorFlow/Keras 模型 __call__ 在 GPU 上运行时变得越来越慢
- mysql - 如何在单个原子操作中将临时表转换为永久表?
- javascript - 我使用 split 和 join 的替换代码仅适用于单个单词输入,而不适用于整个单词
- python - 使用参数化和 xfail 时出现 pytest 错误