Home > Software design >  How to loop through each value in array based on other array and check if it matches some condition
How to loop through each value in array based on other array and check if it matches some condition

Time:02-16

Hi i want to loop through two arrays arr1 and arr2 and check if one value in arr1 matches with each value in arr2 using javascript.

below are the arrays

const arr1 = [
    "item/1/component/1",
    "item/1/",
    "item-group/1/item/1",
    "item/2/component/2",
    "item/2",
    "item-group/2/item/2/component/2/product/1",
] 

const arr2 = [
    "1", 
    "2"
 ]

now i want to loop through each value of arr1 with each value of arr2 and check if it matches with "item/arr2 value"

so for arr2 first value "1" i have to loop through each value of arr1 and check if it matches with string "item/1"

so "item/1/component/1" should be checked with "item/1" and are not equal "item/1/" should be checked wih "item/1" and are equal should do for rest of items in arr1 with first value of arr2. if none of the strings of arr1 matches with arr2 first value it should return true

again should loop through arr1 items with second value of arr2 in this case "2" and check for "item/2" and check if they match. if atleast one value of arr1 match return true

the arr1 should match atleast one string for all values of arr2. if it fails to match for atleast one value of arr2 it should return false. how can i do it? could someone help me with this. thanks.

i am new to programming and trying to find a solution to this from long time and i am unable to solve this. thanks.

what i have tried?

for (let i = 0; i < arr1.length; i  ) {
    for (let j = 0; j < arr2.length; j  ) {
    console.log(arr1[i] === `item/${arr2[j]}/`);
  }
}

thsi will log true or false for each value of arr1. but i want to return true or false for the whole like after looping through all elements in arr2 if there is one failure for arr2 value return false.

CodePudding user response:

You can try following code :

   const arr1 = [
    "item/1/component/1",
    "item/1/",
    "item-group/1/item/1",
    "item/2/component/2",
    "item/2",
    "item-group/2/item/2/component/2/product/1",
] 

const arr2 = [
    "1", 
    "2"
 ]
 
function func(){
     for (let i = 0; i < arr2.length; i  ) {
        for (let j = 0; j < arr1.length; j  ) {
            let temp = "item/" arr2[i];
            if(temp == arr1[j] ){
                return true
            }
      }
    }
    return false
}

console.log(func())

CodePudding user response:

hey not sure what you are looking exactly and I am not an expert to be honest but I just hope this might guide you to continue your challenge:


function checkArrays(array1, array2) {
for(let i=0; i < array1.length; i  ) {
   for(let j=0; j < array2.length; i  ) {
      if(array1.indexOf(array2) !== -1) {
      return true
    } else {
    return false
    }
  }
    return true
  }
}





console.log(checkArrays(
    "item/1/component/1",
    "1"
 ))

You could also check the .some() method which will return true if at least one condition is passing. another way could be to check using the match() method with Regex or .includes() method as well.. :)

  • Related