[英]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.嗨,我想遍历两个 arrays arr1 和 arr2,并使用 javascript 检查 arr1 中的一个值是否与 arr2 中的每个值匹配。
below are the arrays下面是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"现在我想用 arr2 的每个值遍历 arr1 的每个值,并检查它是否与“item/arr2 值”匹配
so for arr2 first value "1" i have to loop through each value of arr1 and check if it matches with string "item/1"所以对于 arr2 第一个值“1”,我必须遍历 arr1 的每个值并检查它是否与字符串“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.所以“item/1/component/1”应该用“item/1”检查并且不等于“item/1/”应该用“item/1”检查并且应该对 arr1 中的 rest 个项目做arr2 的第一个值。 if none of the strings of arr1 matches with arr2 first value it should return true
如果 arr1 的所有字符串都与 arr2 的第一个值不匹配,它应该返回 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.在这种情况下,应该再次循环遍历 arr2 的第二个值为“2”的 arr1 项目,并检查“item/2”并检查它们是否匹配。 if atleast one value of arr1 match return true
如果 arr1 的至少一个值匹配返回 true
the arr1 should match atleast one string for all values of arr2.对于 arr2 的所有值,arr1 应该至少匹配一个字符串。 if it fails to match for atleast one value of arr2 it should return false.
如果它无法匹配 arr2 的至少一个值,它应该返回 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. thsi 将为 arr1 的每个值记录 true 或 false。 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.
但是我想在循环遍历 arr2 中的所有元素后返回 true 或 false,如果 arr2 值返回 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.您还可以检查 the.some() 方法,如果至少有一个条件通过,该方法将返回 true。 another way could be to check using the match() method with Regex or.includes() method as well.. :)
另一种方法是使用 match() 方法和 Regex or.includes() 方法进行检查。:)
Start off by mapping arr2
elements to item2/${a2}
, then for each of these new values check if each element of arr1
.startsWith()
it.首先将
arr2
元素映射到item2/${a2}
,然后针对这些新值中的每一个检查arr1
.startsWith()
的每个元素是否。
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 )) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.