Method one:
function fn(arr){ let result={}; let newArr=[]; for(let i=0; i<arr.length; i++){ if(! result[arr[i]]){ newArr.push(arr[i]); result[arr[i]]=1; } } console.log(newArr); } let arr=['a','a','b','a','c','d']; fn(arr);Copy the code
Output: [” A “, “b”, “c”, “d”]
Method 2:
function fn(st){
let resArr=[];
for(let i in st){
if(resArr.indexOf(st[i]) == -1){
resArr.push(st[i]);
}
}
return resArr;
}
let arr=['aa','ab','b','aa','c','d'];
console.log(fn(arr));
Copy the code
Output: [” AA “, “AB “,” B “, “c”, “D “]
Method 3:
function fn(st){
let arr=new Array();
for(let i in st){
arr[st[i]]=1;
}
let tmparr=new Array();
for(let j in arr){
tmparr.push(j);
}
console.log(arr);
return tmparr;
}
let st=['aa','ab','b','aa','c','d'];
console.log(fn(st));
Copy the code
Output: [” AA “, “AB “,” B “, “c”, “D “]
Method 4:
function fn(st){ st=st||[]; let obj={}; for(let i=0; i<st.length; i++){ let j=st[i]; if(typeof (obj[j]) =='undefined'){ obj[j]=1; } } st.length=0; for(let i in obj){ st[st.length]=i; } return st; } let arr=['aa','ab','b','aa','c','d']; console.log(fn(arr));Copy the code
Output [” AA “, “ab”, “b”, “c”, “d”]