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:
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".
I created the code to check if at least one specific value "Kiwi" exists and it returns "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
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
In addition, this is the case of "Strawberry" which returns "false":
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
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
However, I want to make this code with "Kiwi" simpler. 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
CodePudding user response:
You can use .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)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Its even faster then your loop because .some
stops looping after it finds the searched result
CodePudding user response:
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'))
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Use some() 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
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>