JavaScript. How to Compare Input Arrays

后端 未结 6 1993
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 14:13

I\'m stuck with this problem for 3 days now... Someone please help me.

Challenge 5
Construct a function intersection

6条回答
  •  一整个雨季
    2021-01-15 14:18

    You can go through the first array in the array of arrays and check which of its value is present in all the other arrays.

    Here is an example:

    function intersection(input) {
        let firstArray = input[0];
        let restOfArrays = input.splice(1);
        return firstArray.filter(v => restOfArrays.every(arr => arr.includes(v)));    
    }
    
    const input = [[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]];
    const result = intersection(input);
    
    console.log(result);

提交回复
热议问题