Ask coding questions

← Back to all posts
Getting unique variables in javascript
MikeJMS8910 (234)

I have 4 variables in my javascript code which could sometimes equal the same value so I want to make sure that the list only contains the unique values. If someone could personally help me out with this code that would be great.

Comments
hotnewtop
Coder100 (18882)

Here's the easiest way to remove all duplicates:

function rmDuplicates(arr) {
  return [...new Set(arr)];
}

rmDuplicates([1, 2, 3, 3]) // [1, 2, 3] 
RYANTADIPARTHI (6019)

something like this?

let a = ["1", "1", "2", "3", "3", "1"];
let unique = a.filter((item, i, ar) => ar.indexOf(item) === i);
console.log(unique);
AloegelhiPlaysR (131)

Try this:

array = []
array2 = []

if(array1.includes(array2.length) { 
array1 = 'something different'
}

Untested