简体   繁体   English

检查数组中的所有内部 arrays 是否存在至少一个特定值(JavaScript)

[英]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.

相关问题 javascript-如何检查数组是否至少有一个负值? - javascript - How to check if array has at least one negative value? 使用jQuery / Javascript检查数组中的至少一个输入是否具有值 - Check if at least one input in array has a value using jQuery/Javascript 如何设置一个 arrays 的所有元素必须至少等于 JavaScript 中另一个数组的一个元素的条件? - How to set up consition that all elements of one arrays must be equal to at least one element of the another array in JavaScript? 如何检查数组的至少一项是否包含在 object 的数组中并遍历所有对象(JS) - How to check if at least one item of an array is included in an array of an object and loop through all objects (JS) 检查JavaScript数组中是否存在值 - Check if value exists in JavaScript array 检查标识符javascript所标识的子数组内主数组中的所有元素是否至少出现一次 - Check whether there is at least one occurrence of all the elements in the main array inside the sub array identified by an identifier javascript 检查另一个数组中是否至少存在一个值失败 - Checking if at least one value in one array exists in another fails 我如何通过 JavaScript 中的嵌套 Arrays Map 然后检查嵌套数组的属性之一是否没有值? - How Do I Map through nested Arrays in JavaScript and then check if one of the Nested Array's properties does not have a value? Javascript 检查 object 值中是否存在数组值 - Javascript check if array value exists in object value 如何检查一个Javascript数组,该数组至少包含一个以特定文本开头的值(例如ROLE_) - How to check a Javascript array that contains at least one value that starts with a particular text (eg. ROLE_)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM