覆盖数组中的重复值 - Javascript [英] Overwrite duplicate value in an array - Javascript

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

问题描述

我已经阅读了有关我的问题的部分解决方案,但不幸的是我已经提出质疑。所以这是我的问题。我有一个数组 var = results [1,2,2,3,1,3] 。我必须打印/回显所有的值,最终的值应显示 1,2,3 的值。我已经阅读过有关重复的值,但这可能无法在我的工作,因为它只返回重复值。我已经阅读了关于过滤唯一值,但当然这只会给我一个独特的价值,不会告诉我其他的。而其中一个意见则表明了这一点,绝对可能是一个确切的解决方案:

I have read some partially solutions on my problem but unfortunately I've come up for questioning. So here's my question. I have an array var = results[1,2,2,3,1,3]. I have to print/echo all the values and the final values should be display the value of 1,2,3. I've read about finding values that has a duplicate but this might not work on mine since it only returns duplicate values. I've read about filtering unique values but of course it will only show me a unique value and won't show me the others. And one of the comments there suggests this and absolutely maybe an exact solution:


_。uniq([1,2,1,3, 1,4]);

_.uniq([1, 2, 1, 3, 1, 4]);

=> [1,2,3,4]

=> [1, 2, 3, 4]

链接是 HERE ...

但是我不知道如何正确使用它。是否适用于我的问题?

But I don't know how to use it correctly. Is it applicable to my problem?

这是我的小提琴无论如何,

推荐答案

这是使用js非常简单的实现

Here is the very simple implementation using js

var getfruits = [1, 2, 2, 3, 1, 3];
var uniq = [];
for (var i = 0; i < getfruits.length; i++) {
    if(uniq.indexOf(getfruits[i])==-1)
        uniq.push(getfruits[i]);
};

console.log(uniq);

你必须添加 lo-dash 你的小提琴。

And you have to just add lo-dash in your fiddle.

固定FIDDLE WITH LO-DASH

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

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