2个数组之间的jQuery差异

时间:2018-08-16 13:51:55

标签: javascript jquery arrays javascript-objects

我如何获得两个数组的差?

我认为需要比较索引并删除位置?

数组可以有字母(例如月份)

// Round 1
var array1 = [];
var array2 = [10, 2, 3, 5];
//diff: 10, 2 ,3 ,5

// Round 2
var array1 = [10, 2, 3, 5];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9];
//diff: 2, 5, 11, 9

// Round 3
var array1 = [10, 2, 3, 5, 2, 5, 11, 9];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9, 1, 5, 12, 10];
//diff: 1, 5, 12, 10

1 个答案:

答案 0 :(得分:0)

var array1 = [10, 2, 3, 5, 2, 5, 11, 9];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9, 1, 5, 12, 10];

var diffArray= array2.splice(array1.length,array2.length-array1.length);
console.log(diffArray)

假设array1是array2的子数组