检查用户的值是否存在于给定的数组中。

huangapple go评论47阅读模式
英文:

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&#39;t match
  if(winArray.includes(Number(mergeUserArray))){
    console.log(&quot;Number matched&quot;);
  }
  else {
    console.log(&quot;Number not matched&quot;);
  }

<!-- 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&#39;t match

let match = winArray.some(item =&gt; mergeUserArray.some(what =&gt; {
    const regex = new RegExp(`[${item}]`, &#39;g&#39;);
    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 =&gt; winArray.some(item =&gt; mergeUserArray.some(what =&gt; {
    const regex = new RegExp(`[${item}]`, &#39;g&#39;);
    return what.toString().match(regex)?.join(&#39;&#39;) === 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 =&gt; winArray.some(item =&gt; {
    const regex = new RegExp(`[${item}]`, &#39;g&#39;);
    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 &lt; mergeUserArray.length; i++) {
  const num = mergeUserArray[i].toString();
  if (num.length &gt;= 3) {
    for (let j = 0; j &lt; num.length - 2; j++) {
      const combination = num.substr(j, 3);
      if (winArray.includes(Number(combination))) {
        matchFound = true;
        break;
      }
    }
  }
}

if (matchFound) {
  console.log(&quot;Number matched&quot;);
} else {
  console.log(&quot;Number not matched&quot;);
}

答案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 &amp;&amp; item.toString().localeCompare(item2.toString()) &gt; -1) {
            console.log(`${item} is matched with ${item2}`)
            matchFound = true;
        } else {
            console.log(&#39;Not matched&#39;)
        }
    }

huangapple
  • 本文由 发表于 2023年6月22日 19:44:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定