英文:
Check user value is present in given array
问题
如您在下面的代码中所见。我尝试检查mergeUserArray
的值是否存在于winArray
中。但我无法实现它。
因为我希望,如果mergeUserArray
中有任何其他数字,它应该匹配是否存在三个数字的组合。就像789在7189中存在一样。
我也尝试了正则表达式,但它也没有起作用。
let regxWinArray = /(123)|(456)|(789)/g;
let winArray = [123, 456, 789];
// let mergeUserArray = [789]; --Value matched
let mergeUserArray = [7189]; // 值不匹配
if (winArray.includes(Number(mergeUserArray))) {
console.log("数字匹配");
} else {
console.log("数字不匹配");
}
英文:
As you can see in below code. I'm trying to check whether mergeUserArray
value is present in winArray
or not. But I'm not able to achieve it.
Because I want, if any other number is in mergeUserArray
it should match if three number combination present in it. Like 789 is present in 7189.
I tried regular expression too but it also didn't work.
let regxWinArray = /(123)|(456)|(789)/g;
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let winArray = [123,456,789];
// let mergeUserArray = [789]; --Value matched
let mergeUserArray = [7189]; // Value didn't match
if(winArray.includes(Number(mergeUserArray))){
console.log("Number matched");
}
else {
console.log("Number not matched");
}
<!-- end snippet -->
答案1
得分: 1
使用全局正则表达式字符匹配并检查找到的字符数量是否匹配:
let winArray = [123, 456, 789];
// let mergeUserArray = [789]; --Value matched
let mergeUserArray = [7189]; // 值不匹配
let match = winArray.some(item => mergeUserArray.some(what => {
const regex = new RegExp(`[${item}]`, 'g');
return what.toString().match(regex)?.length === item.toString().length;
}));
console.log(match);
如果字符的顺序应该匹配:
let winArray = [123, 456, 789];
const hasMatch = mergeUserArray => winArray.some(item => mergeUserArray.some(what => {
const regex = new RegExp(`[${item}]`, 'g');
return what.toString().match(regex)?.join('') === item.toString();
}));
console.log([7189], hasMatch([7189]));
console.log([1897], hasMatch([1897]));
如果我们需要匹配mergeUserArray
中的所有值:
let winArray = [123, 456, 789];
let mergeUserArray = [7189, 654];
let match = mergeUserArray.every(what => winArray.some(item => {
const regex = new RegExp(`[${item}]`, 'g');
return what.toString().match(regex)?.length === item.toString().length;
}));
console.log(match);
英文:
Use global regex character match and check whether number of found characters match:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let winArray = [123, 456, 789];
// let mergeUserArray = [789]; --Value matched
let mergeUserArray = [7189]; // Value didn't match
let match = winArray.some(item => mergeUserArray.some(what => {
const regex = new RegExp(`[${item}]`, 'g');
return what.toString().match(regex)?.length === item.toString().length;
}));
console.log(match);
<!-- end snippet -->
If the order of characters should match:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let winArray = [123, 456, 789];
const hasMatch = mergeUserArray => winArray.some(item => mergeUserArray.some(what => {
const regex = new RegExp(`[${item}]`, 'g');
return what.toString().match(regex)?.join('') === item.toString();
}));
console.log([7189], hasMatch([7189]));
console.log([1897], hasMatch([1897]));
<!-- end snippet -->
If we need all values in mergeUserArray
matched:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let winArray = [123, 456, 789];
let mergeUserArray = [7189, 654];
let match = mergeUserArray.every(what => winArray.some(item => {
const regex = new RegExp(`[${item}]`, 'g');
return what.toString().match(regex)?.length === item.toString().length;
}));
console.log(match);
<!-- end snippet -->
答案2
得分: 0
let winArray = [123, 456, 789];
let mergeUserArray = [7189];
let matchFound = false;
for (let i = 0; i < mergeUserArray.length; i++) {
const num = mergeUserArray[i].toString();
if (num.length >= 3) {
for (let j = 0; j < num.length - 2; j++) {
const combination = num.substr(j, 3);
if (winArray.includes(Number(combination))) {
matchFound = true;
break;
}
}
}
}
if (matchFound) {
console.log("Number matched");
} else {
console.log("Number not matched");
}
英文:
let winArray = [123, 456, 789];
let mergeUserArray = [7189];
let matchFound = false;
for (let i = 0; i < mergeUserArray.length; i++) {
const num = mergeUserArray[i].toString();
if (num.length >= 3) {
for (let j = 0; j < num.length - 2; j++) {
const combination = num.substr(j, 3);
if (winArray.includes(Number(combination))) {
matchFound = true;
break;
}
}
}
}
if (matchFound) {
console.log("Number matched");
} else {
console.log("Number not matched");
}
答案3
得分: 0
let winArray = [789, 456, , 123];
let mergeUserArray = [7189];
let matchFound = false;
for (let item of winArray) {
for (let item2 of mergeUserArray) {
if (item && item.toString().localeCompare(item2.toString()) > -1) {
console.log(`${item} is matched with ${item2}`)
matchFound = true;
} else {
console.log('Not matched')
}
}
}
英文:
let winArray = [789, 456, , 123];
let mergeUserArray = [7189];
let matchFound = false;
for (let item of winArray) {
for (let item2 of mergeUserArray) {
if (item && item.toString().localeCompare(item2.toString()) > -1) {
console.log(`${item} is matched with ${item2}`)
matchFound = true;
} else {
console.log('Not matched')
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论