Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

array without duplicates js

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], [];
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #array #duplicates #js
ADD COMMENT
Topic
Name
2+4 =