Home > Web Front-end > JS Tutorial > body text

js sample code for efficiently removing duplicate elements from an array_javascript skills

WBOY
Release: 2016-05-16 17:08:06
Original
1166 people have browsed it
复制代码 代码如下:

function unique(data){
data = data || [];
var a = {};
for (var i=0; ivar v = data[i];
if (typeof(a[v]) == 'undefined'){
a[v] = 1;
}
};
data.length=0;
for (var i in a){
data[data.length] = i;
}
return data;
}

function test(){
var arr = [9,1,3,8,7,7,6,6,5,7,8,8,7,4,3,1];
var arr1 = unique(arr);
alert(arr1.join(","));
}
test();
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template