简体   繁体   中英

How to loop through each value in array based on other array and check if it matches some condition using javascript?

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.

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())

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.. :)

Start off by mapping arr2 elements to item2/${a2} , then for each of these new values check if each element of arr1 .startsWith() it.

 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" ]; arr2.map(a2 => `item/${a2}`).forEach( a2 => arr1.forEach(a1 => console.log(a2, a1, a1.startsWith( a2 )? true: false )) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM