var arr = ["apple", "mango", "apple", "orange", "mango", "mango"];
function removeDuplicates(arr) {
return arr.filter((item,
index) => arr.indexOf(item) === index);
}
console.log(removeDuplicates(arr)); // ["apple", "mango", "orange"]
const Array = [0, 3, 2, 5, 6, 8, 23, 9, 4, 2, 1, 2, 9, 6, 4, 1, 7, -1, -5, 23, 6, 2, 35, 6,
3, 32, 9, 4, 2, 1, 2, 9, 6, 4, 1, 7, 1, 2, 9, 6, 4, 1, 7, -1, -5, 23]
// 1. filter
Array.filter((item, index) => {
return arr.indexOf(item) === index;
});
// 2. set
const newArray = [...new Set(Array)]
console.log(newArray)
// OR //
const newArray = Array.from(new Set(arr))
console.log(newArray)
// 3. reduce
Array.reduce((unique, item) => {
if (unique.includes(item)) {
return unique;
} else return [...unique, item], [];
});
var arr = [[7,3], [7,3], [3,8], [7,3], [7,3], [1,2]];
arr.map(JSON.stringify).reverse().filter((e, i, a) => a.indexOf(e, i+1) === -1).reverse().map(JSON.parse) // [[7,3], [3,8], [1,2]]
function myFunction(myArray, callBack){
var unique = myArray.filter(function(item, pos) {
//validates whether the first occurrence of current item in array
// equals the current position of the item (only return those items)
return myArray.indexOf(item) == pos;
});
//wrap your result and pass to callBack function
callBack(unique);
}