从数组中删除重复的项目 [英] Remove duplicate item from array Javascript

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

问题描述

我正在寻找一种从数组中删除重复值的简单方法。我想出了如何检测是否有重复,只是我不知道如何推它的价值。例如,如果您转到提供的链接,然后键入abca (每个字母后按回车/回车键) ,将会提示重复!



但是我也想弄清楚如何从textarea中删除那个重复的内容?

http://jsfiddle.net/P3gpp/

这似乎是不要工作::

  sort = sort.push(i); 
textVal = sort;
返回textVal;


解决方案

为什么这么难呢,可以做到更容易使用JavaScript过滤功能,这是特别针对这种类型的操作:

$ $ p $ var c arr = [苹果,bannana ,橙,苹果,橙];
$ b $ arr = arr.filter(function(item,index,inputArray){
return inputArray.indexOf(item)== index;
});


---------------------
输出:[apple,bannana,orange ]


I'm looking for an easy way of removing a duplicate value from an array. I figured out how to detect if there is a duplicate or not, just I don't know how to "push" it from the value. For example, if you go to the link provided, and then type, "abca" (press return/enter key after each letter).. it will alert "duplicate!"

But I also want to figure out how to remove that duplicate from the textarea?

http://jsfiddle.net/P3gpp/

This is the part that seems to not be working ::

sort = sort.push(i);
textVal = sort;
return textVal;

解决方案

Why do it the hard way, it can be done more easily using javascript filter function which is specifically for this kind of operations:

var arr = ["apple", "bannana", "orange", "apple", "orange"];

arr = arr.filter( function( item, index, inputArray ) {
           return inputArray.indexOf(item) == index;
    });


---------------------
Output: ["apple", "bannana", "orange"]

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

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