There is an array section = [[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]]; And the array numbers = [2,3,5,8,10,14,15];

How to determine which numbers of numbers are in the section subarray in an amount of more than 1 pc.? For example, the numbers 10, 14 are in the subarray section[2] , i.e. [2, 6, 10, 14 ].

I tried this:

 let result = []; for (let a = 0; a < section.length; a++) { let arr = []; for (let i = 0; i <= numbers.length; i++) { if (section[a].includes(numbers[i])) { arr.push(numbers[i]) } } if(arr.length>1) { result.push(arr); } console.log(result) } 

Did not work. How can I do that?

  • and what should be the output? - Alexey Shimansky
  • @ Alexey Shimansky, an array of numbers. Those. array = [[первое число, второе для первого подмассива], [первое, второе для второго подмассива section],[10, 14]] - user7103883
  • completely incomprehensible ... what is the первое число, второе для первого подмассива and the первое, второе для второго подмассива section ? and why did subarrays become 3? - Alexey Shimansky
  • @ AlekseyShimansky, I am looking for 10, 14 in the section array. If these numbers are in one sub-array (as here [2, 6, 10, 14] ), then add 10, 14 to the result-array. At the output - result = [[10,14]]; - user7103883

3 answers 3

I do not know, here, I think, even recursion is not needed.

 const section = [[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]]; const numbers = [2,3,5,8,10,14,15]; const result = section .map(subs => // map сделает новый массив где значениями будут пересечения значений numbers и подсекции subs.filter(number=>numbers.includes(number)) // просто удалим все значения не входящие в numbers из subs, вернем ).filter(r => r.length>1); //я правильно понимаю, что нужно удалить пустые массивы ? (тут же проверим что длина >1) console.log(result); 

leave for history, if you need a list of numbers all the same

 const section = [[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]]; const numbers = [2,3,5,8,10,14,15]; const result = new Set(); //без Set придется проверять наличие в результатах или держать объект вида {number: true} for (let subs of section) { let found = subs.filter(number=>numbers.includes(number)); if (found.length>1) { found.forEach(number=>result.add(number)); } } console.log(Array.from(result)); 

     var section = [[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]]; var numbers = [2,3,5,8,10,14,15]; function diff(sourceArr, compareArr) { return sourceArr.filter(function(i) { return compareArr.indexOf(i) != -1; }); } var output = []; for (let i = 0; i < section.length; i++) { var diffArr = diff(section[i], numbers); output.push( diffArr.length > 1 ? diffArr : [] ); } console.log(output); 

    diff function - to determine the coincidence of array elements, in which we filter elements if they do not match the condition indexOf(i) != -1 , where indexOf returns the index of the first occurrence of the specified value in the object and returns -1 if no value is found.

    Next in the loop, we apply a function to each section subarray. And if the number of matched elements is> 1, then we put it into output , if not, then we fill in an empty array (if necessary);

      I also did not quite understand everything in your problem. For example, you write in quantities greater than 1 pc. - this means 2, 3, etc., but you, judging by the example, and one entry is suitable? I also did not understand why you put less or equal in the second cycle:

       i <= numbers.length 

      In my opinion, a smaller sign is enough here. Well, here try the solution:

       section = [[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]]; numbers = [2,3,5,8,10,14,15]; var result = []; for (let a = 0; a < section.length; a++) { let arr = []; for (let i = 0; i < numbers.length; i++) { let arrStr = ',' + section[a].join() + ','; let numbStr = ',' + numbers[i] + ','; if ( ~arrStr.indexOf( numbStr ) ) { arr.push(numbers[i]) } } arr.length && result.push(arr); } console.log(result); 
      • Alexey and zb ', you have interesting solutions, but for some reason the resulting array does not include arrays with entries in the first two subarrays of section, the numbers 8 and 5 coincide. If I understood the condition correctly, the correct answer should look something like this: [Array ( 1), Array (1), Array (3), Array (2)]. - Max Kur
      • Well, the condition says> 1 - zb '
      • in the condition it is indeed stated unequivocally that> 1, but then follows a refuting example))). And if we follow the condition> 1, then at the output with such data we get an empty array. The author of the question, only you know, is the correct answer [[8] [5] [2,10,14] [3,15]]? PS: it seems I understood what the author meant. Select arrays where 2 matches and more. - Max Kur