Skip to main content

Posts

Showing posts with the label array

Set and Array in Javascript

 Set:  The set object lets you store unique values of any type const arr1 = [ 1 , 2 , 3 , 1 , 2 , 'name' ] const set1 = new Set(arr1) const arr2 = Array.from(set1) console.log(arr2) // [ 1, 2, 3, 'name' ] add object or array to Set, can't use "has" or "delete" set1.add([ 1 ]) console.log(set1.has([ 1 ])) // false set1.delete([ 1 ]) // false convert to Array const arr2 = Array.from(set1) const arr3 = [...set1] Set for string, remove duplicated letters const text = 'IndiaIndia' ; const mySet = new Set(text); console.log( [...mySet], text.split( '' )) [ 'I', 'n', 'd', 'i', 'a' ] [ 'I', 'n', 'd', 'i', 'a', 'I', 'n', 'd', 'i', 'a' ] Esencia Realty