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).

Source: Internet
Author: User

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).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.