onload = function () {
var a = [2,5,7,9];
var b = [3,4,5,7,8];
Prepare a function to check if an array contains a certain data, return True, do not include and return false
Loop any array (select a here), check every element in the array to B array, do not belong to the difference element, and deposit the new array
Then change the B array and repeat the 2nd step.
var arr = [];
var count = 0;
for (Var i=0;i<a.length;i++) {
if (!checknum (B,a[i])) {
Arr[count] = A[i];
count++;
}
}
for (Var i=0;i<b.length;i++) {
if (!checknum (A,b[i])) {
Arr[count] = B[i];
count++;
}
}
Alert (arr);
};
return function: Returning data and jumping out of functions
/**
* Determine if an array contains a data
* @param {Object} arr
* @param {Object} num
*/
function Checknum (arr,num) {
for (Var i=0;i<arr.length;i++) {
if (Num==arr[i]) {
return true;
}
}
return false;
}
This article from the "11199433" blog, reproduced please contact the author!
Write in JavaScript (find the difference elements in two arrays and put them into a new array, assuming there are no duplicate elements inside each array).