如何在javascript中删除arraylist中的重复项? [英] How to remove duplicates in arraylist in javascript..?

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

问题描述

需要在javascript中删除arraylist中的重复项...



<!DOCTYPE html>





试试吧

Need to remove duplicates in arraylist in javascript...

<!DOCTYPE html>


Try it

推荐答案

你在这里遇到的问题是你只是推动这个名字。更重要的是,您将面临一个问题,即您要查找的内容是一个复杂的类型,因此您将不得不推出自己的实现。幸运的是,这是一件非常简单的事情。首先,你只需要添加你自己的indexOf版本(这样就可以了):
The problem you have here is that you are just pushing the name. More importantly, you are going to face an issue where what you are trying to find is a complex type so you are going to have to roll your own implementation. Fortunately, that's a pretty trivial thing to do. First of all, you would simply add your own version of indexOf (something like this will do):
Array.prototype.complexIndexOf = function(type) {
  for (var x = 0; x < this.length; x++) {
    if (this[x].id === type.id && this[x].name === type.name)
	  return i;
  }
  return -1;
}

使用此原型,您只需将for循环中的条件更改为:

With this prototype in place, you simply change the condition in your for loop to:

if (t.complexIndexOf(array[x]))t.push(array);


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

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