Home > Web Front-end > JS Tutorial > JS implementation of merging two arrays and removing duplicates, leaving only one method_javascript skills

JS implementation of merging two arrays and removing duplicates, leaving only one method_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:25:17
Original
2275 people have browsed it

The example in this article describes the JS method of merging two arrays and removing duplicates, leaving only one. Share it with everyone for your reference, the details are as follows:

//It's merge arr1 and arr2 , delete the same element only leave one
//It's only apdapter array. If object, no. 
//The sequence of the two array is not required.
mergeArray:function (arr1, arr2){ 
 for (var i = 0 ; i < arr1.length ; i ++ ){
   for(var j = 0 ; j < arr2.length ; j ++ ){
    if (arr1[i] === arr2[j]){
     arr1.splice(i,1); //利用splice函数删除元素,从第i个位置,截取长度为1的元素
    }
   }
 }
 //alert(arr1.length)
 for(var i = 0; i <arr2.length; i++){
  arr1.push(arr2[i]);
 }
 return arr1;
}

Copy after login

Another: The premise is that the items in the two arrays are not repeated. If they are repeated, the desired effect will not be achieved

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template