[英]Check if at least one specific value exists looking through all inner arrays in an array (JavaScript)
I have the array "arr" which contains 2 objects and each object has the array "innerArr" which contains 2 or 3 objects with one value "val" for each object:我有一个包含 2 个对象的数组“arr” ,每个 object 都有一个数组“innerArr” ,其中包含 2 个或 3 个对象,每个 object 有一个值“val” :
let arr = [
{
innerArr: [
{ val: "Apple" },
{ val: "Grape" }
]
},
{
innerArr: [
{ val: "Peach" },
{ val: "Mango" },
{ val: "Kiwi" }
]
}
];
Now, I want to check if at least one specific value like "Kiwi" exists looking through all inner arrays "innerArr" in the array "arr" .现在,我想通过数组"arr"中的所有内部 arrays "innerArr"检查是否存在至少一个特定值,例如 "Kiwi "。
I created the code to check if at least one specific value "Kiwi" exists and it returns "true" .我创建了代码来检查是否存在至少一个特定值 "Kiwi"并返回"true" 。
let result = false;
for(let i = 0; i < arr.length; i++) {
for(let j = 0; j < arr[i].innerArr.length; j++) {
if(arr[i].innerArr[j].val == "Kiwi") { // Check if "Kiwi" exists
result = true;
}
}
}
console.log(result); // true
This is the full runnable code :这是完整的可运行代码:
let arr = [ { innerArr: [ { val: "Apple" }, { val: "Grape" } ] }, { innerArr: [ { val: "Peach" }, { val: "Mango" }, { val: "Kiwi" } ] } ]; let result = false; for(let i = 0; i < arr.length; i++) { for(let j = 0; j < arr[i].innerArr.length; j++) { if(arr[i].innerArr[j].val == "Kiwi") { // Check if "Kiwi" exists result = true; } } } console.log(result); // true
In addition, this is the case of "Strawberry" which returns "false" :此外,这是返回"false"的"Strawberry"的情况:
let arr = [ { innerArr: [ { val: "Apple" }, { val: "Grape" } ] }, { innerArr: [ { val: "Peach" }, { val: "Mango" }, { val: "Kiwi" } ] } ]; let result = false; for(let i = 0; i < arr.length; i++) { for(let j = 0; j < arr[i].innerArr.length; j++) { if(arr[i].innerArr[j].val == "Strawberry") { // Check if "Strawberry" exists result = true; } } } console.log(result); // false
However, I want to make this code with "Kiwi" simpler.但是,我想用“Kiwi”使这段代码更简单。 Are there any ways to make this code simpler?
有没有办法让这段代码更简单?
let result = false;
for(let i = 0; i < arr.length; i++) {
for(let j = 0; j < arr[i].innerArr.length; j++) {
if(arr[i].innerArr[j].val == "Kiwi") { // Check if "Kiwi" exists
result = true;
}
}
}
console.log(result); // true
You can use .some
您可以使用
.some
let arr = [ { innerArr: [ { val: "Apple" }, { val: "Grape" } ] }, { innerArr: [ { val: "Peach" }, { val: "Mango" }, { val: "Kiwi" } ] } ]; let fruit = "Kiwi" let result = arr.some(({ innerArr }) => innerArr.some(({val}) => val === fruit)) console.log(result)
Its even faster then your loop because .some
stops looping after it finds the searched result它比你的循环更快,因为
.some
在找到搜索结果后停止循环
let arr = [ { innerArr: [ { val: "Apple" }, { val: "Grape" } ] }, { innerArr: [ { val: "Peach" }, { val: "Mango" }, { val: "Kiwi" } ] } ]; let fruits = arr.flatMap(e => e.innerArr).map(e => e.val) console.log(fruits) console.log(fruits.includes('Kiwi'))
Use some() function :使用一些() function :
let arr = [ { innerArr: [ { val: "Apple" }, { val: "Grape" } ] }, { innerArr: [ { val: "Peach" }, { val: "Mango" }, { val: "Kiwi" } ] } ]; // Check if "Kiwi" exists let result = arr.some(v1 => v1.innerArr.some(v2 => v2.val == "Kiwi")); console.log(result); // true // Check if "Strawberry" exists result = arr.some(v1 => v1.innerArr.some(v2 => v2.val == "Strawberry")); console.log(result); // false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.