简体   繁体   English

从数组获取唯一值

[英]Get Unique Values from Array

var arr = ["THYROID","CARDIAC","CARDIAC,DIABETES","METABOLIC,ARTHRITIS","RENAL DISEASES,DIABETES","LIVER DISEASES,HEPATITIS","LIVER DISEASES,CANCER,METABOLIC","LIVER DISEASES,HEPATITIS,ARTHRITIS,METABOLIC"]

Above is my Code, I need to fetch unique Values from this array using Javascript. 上面是我的代码,我需要使用Javascript从此数组中获取唯一值。

Like, what i expect is: If i ask for Unique Values , it should get me : 就像,我期望的是:如果我要求唯一值,它应该让我:

var arr = ["THYROID","CARDIAC","DIABETES","METABOLIC","RENAL DISEASES","LIVER DISEASES,"CANCER",ARTHRITIS","HEPATITIS"]

If you have an array of strings, you can just create an object using those strings as keys, then get the keys from that object. 如果您具有字符串数组,则可以仅使用这些字符串作为键来创建对象,然后从该对象获取键。

Create the object 创建对象

for (var t = {}, i = 0; i < arr.length; i++) 
  t[arr[i]] = 0;

Create an array from the objects keys 从对象键创建数组

// Modern browsers only
arr = Object.keys(t);

// In older browsers you can use the polyfill from the link below,
// or just loop through the object like this:
arr = [];
for (var k in t)
  arr.push(k);

Object.keys Object.keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM