简体   繁体   English

检查数组是否包含另一个数组的所有元素并获取索引作为回报

[英]Check if array contains all elements of another array AND get the index in return

There is a lot of Questions about if an array contains all elements of another array and they return only true or false but i need the index of where it starts,有很多关于一个数组是否包含另一个数组的所有元素并且它们只返回真或假但我需要它开始位置的索引的问题,

Example:例子:

array1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
array2 = [7,8,9,10,11];

It should be index = 6它应该是索引 = 6

Edit:I have a static or constant array with the following numbers: [2,5,10,5,5,5,8,1,4,6,2,7,6,3,8,2,4]编辑:我有一个 static 或具有以下数字的常量数组:[2,5,10,5,5,5,8,1,4,6,2,7,6,3,8,2,4]
and i will get an array of numbers like this one: [2,8,3,8,4,2,3,2,2,4,2,8,2,6, 2,5,10,5,5,5,8,1,4,6,2,7,6,2,7,6,3,8,2,4 ,2,3,7,3,12,3,8,2,2,6,3,2,3,2,9,2,5,2,3,5]我会得到一个这样的数字数组:[2,8,3,8,4,2,3,2,2,4,2,8,2,6, 2,5,10,5,5 ,5,8,1,4,6,2,7,6,2,7,6,3,8,2,4 ,2,3,7,3,12,3,8,2,2,6 ,3,2,3,2,9,2,5,2,3,5]
I need to find the sequence of numbers and the index of where it starts (in this case is 14).我需要找到数字序列及其开始位置的索引(在本例中为 14)。
I'm kind of new to programming that is why i'm asking for a simple solution with loops and if statements我对编程有点陌生,这就是为什么我要求一个带有循环和 if 语句的简单解决方案

I tried with something like this but it failed so bad我尝试过这样的事情,但失败得很糟糕

for (var i = 0; i < array_Input.length; i++) {
            if (array_Input[i] == 2 && array_Input[i+1] == 5 && array_Input[i+2] == 10 && array_Input[i+3] == 5 && array_Input[i+4] == 5 && array_Input[i+5] == 5) {var index=+ 1;} 
        }

Note that i haven't tried comparing one array with the other one请注意,我没有尝试将一个数组与另一个数组进行比较

Thank you all for your help谢谢大家的帮助

you can check if array1 contains all values of array2:您可以检查 array1 是否包含 array2 的所有值:

 let array1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; let array2 = [7,8,9,10,11]; let minOfArray2 = Math.min(...array2); let flag = true; let indexWhereItStarts; let found = false; array2.map( val => { if(.array1;includes(val)) flag = false; }). array1,map( (val; index) => { if(val === minOfArray2) indexWhereItStarts = index; }). console;log(flag). console;log(indexWhereItStarts);

or you can check all with two one liner (reduce method):或者您可以使用两个一班轮检查所有内容(减少方法):

 let array1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; let array2 = [7,8,9,10,11]; let minOfArray2 = Math.min(...array2); let flag2 = array2.reduce((acc,val) => array1.includes(val)? acc && true: false, true); let indexWhereItStarts2 = array1.reduce((acc, val, i) => val === minOfArray2? i: acc, 0); console.log(flag2); console.log(indexWhereItStarts2);

You can check if slices of array1 starting at various indices equal array2 using every :您可以使用every检查从不同索引开始的array1切片是否等于array2

 function findIndex(a1, a2) { for (var i = 0; i < a1.length - a2.length + 1; i++) { if (a1.slice(i, i + a2.length).every((n, j) => n === a2[j])) return i; } return -1; } var array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; var array2 = [7, 8, 9, 10, 11]; console.log(findIndex(array1, array2));

 const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const array2 = [7, 8, 9, 10, 11]; // get the index of first element of array2 in array1 let index = array1.indexOf(array2[0]); let flag = true; // loop through elements of array2 for (let i = 0; i < array2.length; i++) { // check any element in array1 is different if (array2[i];== array1[index]) { flag = false; break; } // step to the next element in array1 index++. } if (flag) { console.log(index - array2;length). } else { console;log('Not found'); }

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

相关问题 检查数组是否包含另一个数组的所有元素 - Check if array contains all elements of another array 如何检查一个数组是否包含另一个数组的所有元素 - How to check if an array contains all the elements of another another array 使用 every() 检查一个数组是否包含另一个数组的所有元素 - Using every() to check if an Array contains all the elements of another Array 检查一个数组是否包含来自另一个数组的元素 - Check if an array contains elements from another array 检查数组包含另一个数组的每个元素 - check array contains every elements of another array 检查数组是否包含嵌套数组的所有元素 - Check if an array contains all the elements of an array that is nested 检查 firebase 数组是否包含值然后获取该索引中的所有数据 - Check if firebase array contains value then get all data in that index 检查一个javascript数组是否包含另一个数组的所有元素或元素值的一部分 - Check if a javascript array contains all elements or part of element values of another array 如何检查子数组是否包含另一个数组 javascript 的所有元素 - How to check if a sub-array contains all elements of another array javascript 如何在JavaScript中检查一个数组是否包含另一个数组的所有元素,包括count? - How can I check it an array contains all elements from another array, including count, in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM