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

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

问题描述

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

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!"

但我也想弄清楚如何从 textarea 中删除重复项?

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;

推荐答案

为什么这么难,使用专门针对此类操作的javascript过滤功能可以更轻松地完成:

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"]

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

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