简体   繁体   English

将数字数组与“数字数组数组”进行比较以查找匹配项

[英]Compare number array with 'array of number arrays' to find matches

I'm trying something I've never attempted before and I'm stumped on finding the solution (hopefully I'm not attempting the impossible!). 我正在尝试一些以前从未尝试过的方法,却为寻找解决方案而烦恼(希望我没有尝试不可能的事情!)。

I have an array containing 24 individual arrays, each containing 4 number values each. 我有一个包含24个单独数组的数组,每个数组包含4个数字值。

var arrayOfArrays = [
    [ 0, 7, 14, 21 ],
    [ 1, 8, 15, 22 ],
    [ 2, 9, 16, 23 ],
    [ 6, 13, 20, 27 ] and so on for 24 arrays.

I also have an empty array, which has a new number pushed into it on a click event. 我也有一个空数组,在单击事件中有一个新数字被推入其中。 Each click adds a new number ie 每次点击都会添加一个新数字,即

var userGeneratedArray = []
- click
[7]
- click
[7, 32]
-click
[7, 32, 14]
-click
[7, 32, 14, 24]
-click
[7, 32, 14, 24, 34]

etc. 等等

What I want to do is: 我想做的是:

1) Every time a new number is added to userGeneratedArray, I want to loop through arrayOfArrays and compare numbers. 1)每次将新数字添加到userGeneratedArray时,我都要遍历arrayOfArrays并比较数字。

2) Once ANY four numbers in userGeneratedArray match ALL of the numbers in ANY of the arrays within arrayOfArrays, return true. 2)一旦userGeneratedArray中的任何四个数字与arrayOfArrays中的任何数组中的所有数字匹配,则返回true。

Any idea of how to construct this loop? 关于如何构造此循环的任何想法? Any help would be greatly appreciated :). 任何帮助将不胜感激 :)。

This is my full array of arrays 这是我完整的数组数组

    var arrayOfArrays = [
        [ 0, 7, 14, 21 ],
        [ 1, 8, 15, 22 ],
        [ 2, 9, 16, 23 ],
        [ 6, 13, 20, 27 ],

        [ 7, 14, 21, 28 ],
        [ 8, 15, 22, 29 ],
        [ 12, 19, 26, 33 ],
        [ 14, 21, 28, 35 ],

        [ 18, 25, 32, 39 ],
        [ 19, 26, 33, 40 ],
        [ 20, 27, 34, 41 ],
        [ 36, 31, 26, 21 ],

        [ 37, 32, 27, 22 ],
        [ 38, 33, 28, 23 ],
        [ 30, 25, 20, 15 ],
        [ 31, 26, 21, 16 ],

        [ 32, 27, 22, 17 ],
        [ 24, 19, 13, 9 ],
        [ 25, 20, 15, 10 ],
        [ 26, 21, 16, 11 ],

        [ 18, 13, 8, 3 ],
        [ 19, 14, 9, 4 ],
        [ 20, 15, 10, 5 ],
        [ 13, 20, 27, 34 ]
    ];

You could create a data structure that efficiently identifies the arrays in which a selected number occurs (avoiding to search them over and over again): a mapping of each selectable number to a list of array-objects that contain it. 您可以创建一个数据结构,以有效地标识其中出现选定编号的数组(避免一遍又一遍地搜索它们):每个可选编号到包含该编号的数组对象列表的映射。 Add a counter to each such object to track whether all were selected. 向每个此类对象添加一个计数器,以跟踪是否全部选中。

Here is how that could look. 这是可能的样子。 I added buttons with which the numbers can be selected: 我添加了可以选择数字的按钮:

 const arrayOfArrays = [[ 0, 7, 14, 21 ],[ 1, 8, 15, 22 ],[ 2, 9, 16, 23 ],[ 6, 13, 20, 27 ],[ 7, 14, 21, 28 ],[ 8, 15, 22, 29 ],[ 12, 19, 26, 33 ],[ 14, 21, 28, 35 ],[ 18, 25, 32, 39 ],[ 19, 26, 33, 40 ],[ 20, 27, 34, 41 ],[ 36, 31, 26, 21 ],[ 37, 32, 27, 22 ],[ 38, 33, 28, 23 ],[ 30, 25, 20, 15 ],[ 31, 26, 21, 16 ],[ 32, 27, 22, 17 ],[ 24, 19, 13, 9 ],[ 25, 20, 15, 10 ],[ 26, 21, 16, 11 ],[ 18, 13, 8, 3 ],[ 19, 14, 9, 4 ],[ 20, 15, 10, 5 ],[ 13, 20, 27, 34 ]]; // Create efficient data structure for counting the selections const numberToCounters = Array.from({length: 42}, () => []); arrayOfArrays.map((arr) => ({ arr, selected: 0 })) .forEach(counter => counter.arr.forEach(k => numberToCounters[k].push(counter)) ); // Function to process a selection and see if 4 of the same array const select = num => numberToCounters[num].find(counter => ++counter.selected == counter.arr.length); // I/O handling for (let k = 0; k <= 41; k++) { let button = document.createElement("button"); button.textContent = k; button.className = "choice"; document.body.append(button); } document.addEventListener("click", function (e) { const button = e.target; if (button.className !== "choice") return; button.disabled = true; const counter = select(button.textContent); if (counter) { // game over for (let button of document.querySelectorAll(".choice")) button.disabled = true; document.querySelector("#output").textContent = "Matched all values in " + counter.arr; } }); 
 <div id="output"></div> 

You want the user to enter some array in the input and match with the arrays you've given completely and then return that array which is matched. 您希望用户在输入中输入某个数组,并与您完全给定的数组匹配,然后返回匹配的数组。 This is very easy and can be done in steps 这非常简单,可以分步完成

  1. First, add a keyup to your input and when the user type, split the string into an array. 首先,在您的输入中添加一个keyup输入,然后在用户键入内容时,将字符串拆分为一个数组。 There you have your array by the input 在那里,您可以通过输入获得数组
  2. Then forEach over the arrayOfArrays to get every value inside. 然后forEacharrayOfArrays上获取每个值。
  3. Now forEach over the elements of arrayOfArrays and then do a every loop on the arrays. 现在, forEach遍历arrayOfArrays的元素,然后对数组进行every遍历。 Check if every value matches the array if it does then return your answer 检查每个值是否均与数组匹配,然后返回答案

 var arrayOfArrays = [ [0, 7, 14, 21], [1, 8, 15, 22], [2, 9, 16, 23], [6, 13, 20, 27], [7, 14, 21, 28], [8, 15, 22, 29], [12, 19, 26, 33], [14, 21, 28, 35], [18, 25, 32, 39], [19, 26, 33, 40], [20, 27, 34, 41], [36, 31, 26, 21], [37, 32, 27, 22], [38, 33, 28, 23], [30, 25, 20, 15], [31, 26, 21, 16], [32, 27, 22, 17], [24, 19, 13, 9], [25, 20, 15, 10], [26, 21, 16, 11], [18, 13, 8, 3], [19, 14, 9, 4], [20, 15, 10, 5], [13, 20, 27, 34] ]; let answer = [] document.querySelector("#val").onkeyup = function() { let array_user = this.value.split(',') // convert input value to array arrayOfArrays.forEach((array) => { // for every array inside arrayOfArrays let trues = array.every((val, index) => { // match every element inside the arrays of the variable arrayOfArrays and then return the condition return val == array_user[index] }) if (trues){ // if your answer is correct then say yes console.log("ANSWER IS THIS => " + array) } }) } 
 <input id="val"> 

For instance, type 13, 20, 27, 34 in the input (or any other values from the array dictionary) and this will match the arrays inside of it. 例如,式13, 20, 27, 34在输入(或从阵列字典任何其他值),这将匹配它内部的阵列。

The approach in this snippet is to filter arrayOfArrays using the input array values. 此代码段中的方法是使用输入数组值过滤arrayOfArrays If the filtered result has arrays of length 4, four numbers from the input array match one of the arrays of arrayOfArrays . 如果过滤结果的数组长度为4,则输入数组中的四个数字与arrayOfArrays数组之一arrayOfArrays

 const arrayFromInput = [23,55,22,0,7,13,7,22,16,13,21,14]; const otherArrayFromInput = [23,55,22,1,14,15,98,6,7]; const log = str => document.querySelector("#result").textContent += `${str}\\n`; const arrayOfArrays = [ [ 0, 7, 14, 21 ], [ 1, 8, 15, 22 ], [ 2, 9, 16, 23 ], [ 6, 13, 20, 27 ], // [...] ]; checkArrayExistence(arrayFromInput); checkArrayExistence(otherArrayFromInput); function checkArrayExistence(inputValues) { let report = `Check: any 4 of [${inputValues}] in one of arrayOfArrays?`; // from a HTML input field the values may be strings, so convert first intermediateArray = inputValues.map(Number); // for every array of arrayOfArrays, filter for every value of [input] const checked = arrayOfArrays .reduce( (reduced, current) => reduced.concat([current.filter(v => intermediateArray.includes(v))]), [] ) .filter(v => v.length === 4); log(`${report} ${checked.length ? "YEP" : "NOPE"}`); if (checked.length) { log(` => Matching arrays in arrayOfArrays: ${JSON.stringify(checked)}`); } }; 
 <pre id="result"></pre> 

  var arrayOfArrays = [ [ 0, 7, 14, 21 ], [ 1, 8, 15, 22 ], [ 2, 9, 16, 23 ], [ 6, 13, 20, 27 ], [ 7, 14, 21, 28 ], [ 8, 15, 22, 29 ], [ 12, 19, 26, 33 ], [ 14, 21, 28, 35 ], [ 18, 25, 32, 39 ], [ 19, 26, 33, 40 ], [ 20, 27, 34, 41 ], [ 36, 31, 26, 21 ], [ 37, 32, 27, 22 ], [ 38, 33, 28, 23 ], [ 30, 25, 20, 15 ], [ 31, 26, 21, 16 ], [ 32, 27, 22, 17 ], [ 24, 19, 13, 9 ], [ 25, 20, 15, 10 ], [ 26, 21, 16, 11 ], [ 18, 13, 8, 3 ], [ 19, 14, 9, 4 ], [ 20, 15, 10, 5 ], [ 13, 20, 27, 34 ] ]; newArrays = []; var noMatch = false; var addedNumbers = []; function addNumber() { if(noMatch || addedNumbers.length === 4) { return; } let value = document.querySelector('input').value; if(value) { addedNumbers.push(value); document.querySelector('span').textContent = [...addedNumbers]; value = parseInt(value); if(newArrays.length == 0) { newArrays = arrayOfArrays.filter(array => { return array.some(item => item == value); }); } else { newArrays = newArrays.filter(array => { return array.some(item => item == value); }); } document.querySelector('input').value = ''; if(addedNumbers.length === 4 && newArrays.length > 0) { alert('match found'); } if(newArrays.length === 0) { noMatch = true; alert('No match found'); } console.log(newArrays); } } 
 <input type="number"> <button onclick="addNumber()">Add Numbers</button> <span></span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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