2014-01-29 183 views
0
var arr = ["THYROID","CARDIAC","CARDIAC,DIABETES","METABOLIC,ARTHRITIS","RENAL DISEASES,DIABETES","LIVER DISEASES,HEPATITIS","LIVER DISEASES,CANCER,METABOLIC","LIVER DISEASES,HEPATITIS,ARTHRITIS,METABOLIC"] 

以上是我的代碼, 我需要使用Javascript從此數組中獲取唯一值。從數組中獲取唯一值

一樣,我的期望是: 如果我要獨特價值,就應該讓我:

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

回答

1

如果你有一個字符串數組,你可以創建一個使用這些字符串作爲鍵的對象,然後從該對象獲取密鑰。

創建對象

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

創建從所述對象鍵陣列

// 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

+0

尼斯思想.. + 1 –