javascript sort with no multiple entry

  • A+
Category:Languages

Here is my code

var array1 = [1,1, 30, 4, 21]; array1.sort(); console.log(array1); // expected output: Array [1, 21, 30, 4]

I want to get the result [1,4,21,30]. the sort function should have give expected result [1,1,4,21,30] it is not giving that . Furthermore what approaches should i take just to keep one "1"? as I dont want duplications

javascript sort with no multiple entry


After sort you can filter your array to get rid of duplicates.

Here

var array1 = [1,1, 30, 4, 21]; array1.sort((a, b) => a - b); var s = array1.filter(function(i, o) {     return array1.indexOf(i) == o; });  console.log(s)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: