删除重复的数组项 [英] remove duplicated array item

查看:130
本文介绍了删除重复的数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  VAR ARR = [吊灯,大女孩惊魂,烧页,人质,直取刀,针眼, 公平游戏,弹性的心,自由的动物,火相约汽油,玻璃纸,一袭黑衣,吊灯,弹性的心,吊灯,吊灯,弹性的心,弹性的心,大女孩惊魂,大女孩惊魂]; $。每个(ARR,功能(我,OBJ){
的console.log(OBJ);
});

我如何确保我的数组是独一无二的?


解决方案

通过的 阵列#过滤器() 并临时对象。

\r
\r

VAR ARR = [吊灯,大女孩惊魂,烧页,眼针的状况,人质,直取刀,公平游戏,弹性的心,自由的动物,火相约汽油,玻璃纸,披着黑,吊灯,弹性的心,吊灯,吊灯,弹性的心,弹性的心,大女孩惊魂,大女孩惊魂]\r
    独特= arr.filter(函数(){\r
        如果(!这[一]){\r
            该[A] =真;\r
            返回true;\r
        }\r
    },{});\r
    \r
的document.write('< pre>'+ JSON.stringify(独特,0,4)+'< / pre>');

\r

\r
\r

var arr = ["Chandelier", "Big Girls Cry", "Burn the Pages", "Eye of the Needle", "Hostage", "Straight for the Knife", "Fair Game", "Elastic Heart", "Free the Animal", "Fire Meet Gasoline", "Cellophane", "Dressed In Black", "Chandelier", "Elastic Heart", "Chandelier", "Chandelier", "Elastic Heart", "Elastic Heart", "Big Girls Cry", "Big Girls Cry"];

 $.each(arr, function(i,obj){
console.log(obj);
});

How can I make sure my array is unique?

解决方案

With Array#filter() and a temporary object.

var arr = ["Chandelier", "Big Girls Cry", "Burn the Pages", "Eye of the Needle", "Hostage", "Straight for the Knife", "Fair Game", "Elastic Heart", "Free the Animal", "Fire Meet Gasoline", "Cellophane", "Dressed In Black", "Chandelier", "Elastic Heart", "Chandelier", "Chandelier", "Elastic Heart", "Elastic Heart", "Big Girls Cry", "Big Girls Cry"],
    unique = arr.filter(function (a) {
        if (!this[a]) {
            this[a] = true;
            return true;
        }
    }, {});    
    
document.write('<pre>' + JSON.stringify(unique, 0, 4) + '</pre>');

这篇关于删除重复的数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆