Home  >  Article  >  Web Front-end  >  Specific implementation of obtaining non-duplicate values ​​​​in 3 arrays_javascript skills

Specific implementation of obtaining non-duplicate values ​​​​in 3 arrays_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:06:061114browse
Copy code The code is as follows:

var a = [ "a" , "b" , "c" ],
b = [ "b" , "c" , "d" ],
c = [ "c" , "d" , "e" ],
_a = a.concat( b ).concat( c ),
_hash = {},
_new = [];
for( var i = _a.length; i--; ){
if( !_hash[ _a [ i ] ] ){
_hash[ _a[ i ] ] = 1;
_new.push( _a[ i ] );
};
};
return _new;

Idea: First use concat to splice arrays, and then use an object and a new array (used to store non-duplicate arrays).

Traverse the old array and put the values ​​into the object. If the values ​​are different, they will be put into the new array. If they are repeated, they will not be placed.
Statement:
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