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

问题描述

我有一个数组

var a =["color", "radius", "y", "x", "x", "x"];

如何检查此数组没有相同的元素?

How to check, that this array does not have the same elements?

推荐答案

尝试一下,

var a = ["color", "radius", "y", "x", "x", "x"];
var uniqueval = a.filter(function (itm, i, a) {// array of unique elements
    return i == a.indexOf(itm);
});
if (a.length > uniqueval.length) {
    alert("duplicate elements")
}
else{
    alert('Unique elements')
}

演示,其中重复唯一元素

这篇关于我需要检查一个JavaScript数组,看看是否有重复的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:50