是否有一种方法可以在JavaScript中返回两个数组之间的差异?
例如:
var a1 = ['a', 'b'];
var a2 = ['a', 'b', 'c', 'd'];
// need ["c", "d"]
是否有一种方法可以在JavaScript中返回两个数组之间的差异?
例如:
var a1 = ['a', 'b'];
var a2 = ['a', 'b', 'c', 'd'];
// need ["c", "d"]
当前回答
function arrayDiff(a, b) {
return a.concat(b).filter(val => !(b.includes(val)));
//(or) return a.concat(b).filter(val => !(a.includes(val) && b.includes(val)));
}
其他回答
您可以使用一个公共对象并计算第一个数组中每个值的频率。对于第二个数组,减少公共对象中的值。然后遍历所有键并添加所有值大于1的键。
常量差值= (a1, a2) => { Var obj = {}; a1。forEach(obj[v] = (obj[v] || 0) + 1); a2。forEach(v => obj[v] = (obj[v] || 0) - 1); 返回对象 . keys (obj) .reduce((r,k) => { If (obj[k] > 0) r = r.concat (Array.from({长度:obj [k]}) .fill (k)); 返回r; }, []); }; const =结果不同([' a ', ' ', ' b ', ' c ', ' d '], [a, b]); console.log(结果);
var arrayDifference = function(arr1, arr2){
if(arr1 && arr1.length){
if(arr2 && arr2.length > 0){
for (var i=0, itemIndex; i<arr2.length; i++){
itemIndex = arr1.indexOf(arr2[i]);
if(itemIndex !== -1){
arr1.splice(itemIndex, 1);
}
}
}
return arr1;
}
return [];
};
arrayDifference([1,2,3,4,5], [1,5,6]);
类似于Ian Grainger的解决方案(但在打字稿中):
function findDiffs(arrayOne: string[], arrayTwo: string[]) {
let onlyInArrayOne = []
let onlyInArrayTwo = []
let share = []
let [arrayOneCopy, arrayTwoCopy] = [[...arrayOne], [...arrayTwo]]
arrayOneCopy.sort(); arrayTwoCopy.sort()
while (arrayOneCopy.length !== 0 && arrayTwoCopy.length !== 0) {
if (arrayOneCopy[0] == arrayTwoCopy[0]) {
share.push(arrayOneCopy[0])
arrayOneCopy.splice(0, 1)
arrayTwoCopy.splice(0, 1)
}
if (arrayOneCopy[0] < arrayTwoCopy[0]) {
onlyInArrayOne.push(arrayOneCopy[0])
arrayOneCopy.splice(0, 1)
}
if (arrayOneCopy[0] > arrayTwoCopy[0]) {
onlyInArrayTwo.push(arrayTwoCopy[0])
arrayTwoCopy.splice(0, 1)
}
}
onlyInArrayTwo = onlyInArrayTwo.concat(arrayTwoCopy)
onlyInArrayOne = onlyInArrayOne.concat(arrayOneCopy)
return {
onlyInArrayOne,
onlyInArrayTwo,
share,
diff: onlyInArrayOne.concat(onlyInArrayTwo)
}
}
// arrayOne: [ 'a', 'b', 'c', 'm', 'y' ]
// arrayTwo: [ 'c', 'b', 'f', 'h' ]
//
// Results:
// {
// onlyInArrayOne: [ 'a', 'm', 'y' ],
// onlyInArrayTwo: [ 'f', 'h' ],
// share: [ 'b', 'c' ],
// diff: [ 'a', 'm', 'y', 'f', 'h' ]
// }
function array_diff(a, b) {
let array = [];
for(let i = 0; i <a.length; i++) {
let k = 0;
for( let j = 0; j < b.length; j++) {
if(a[i]!==b[j]) {
k++;
}
if(k===b.length) {
array = array.concat(a[i]);
}
}
if(b.length ===0) {
array = array.concat(a[i]);
}
}
return array;
}
下面是另一个可以返回差异的解决方案,就像git diff一样:(它已经用typescript编写,如果你不使用typescript版本,只需删除类型)
/**
* util function to calculate the difference between two arrays (pay attention to 'from' and 'to'),
* it would return the mutations from 'from' to 'to'
* @param { T[] } from
* @param { T[] } to
* @returns { { [x in string]: boolean } } it would return the stringified version of array element, true means added,
* false means removed
*/
export function arrDiff<T>(from: T[], to: T[]): { [x in string]: boolean } {
var diff: { [x in string]: boolean } = {};
var newItems: T[] = []
diff = from.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {})
for (var i = 0; i < to.length; i++) {
if (diff[JSON.stringify(to[i])]) {
delete diff[JSON.stringify(to[i])]
} else {
newItems.push(to[i])
}
}
return {
...Object.keys(diff).reduce((a, e) => ({ ...a, [e]: false }), {}),
...newItems.reduce((a, e) => ({ ...a, [JSON.stringify(e)]: true }), {})
}
}
下面是一个用法示例:
arrDiff(['a', 'b', 'c'], ['a', 'd', 'c', 'f']) //{"b": false, "d": true, "f": true}