DEV Community

sakethk
sakethk

Posted on • Updated on

Learn Javascript array methods with the help of emojis

//Concat
['πŸ‹πŸ»β€β™‚οΈ', 'πŸƒπŸ»'].concat('🧘🏻') = [ 'πŸ‹πŸ»β€β™‚οΈ', 'πŸƒπŸ»', '🧘🏻' ] 

//Join
['🀴🏻', 'πŸ‘ΈπŸ»'].join('πŸ’') = 'πŸ€΄πŸ»πŸ’πŸ‘ΈπŸ»'

//Slice
['😭', '😢', 'πŸ˜€'].slice(2)= [ 'πŸ˜€' ]

//Index of
['0️⃣', '1️⃣', '2️⃣', '3️⃣'].indexOf('1️⃣') = 1

//Includes
['0️⃣', '1️⃣', '2️⃣', '3️⃣'].includes('1️⃣') = true

//Every
[
  {label: '0️⃣', type: 'emoji'}, 
  {label: '1️⃣', type: 'emoji'}, 
  {label: '2️⃣', type: 'emoji'}, 
  {label: '3️⃣', type: 'emoji'}
].every(item => item.type === 'emoji') = true

//Some
[0, '1️⃣', '2️⃣', '3️⃣'].some(item => typeof item === 'number') = true

//Fill
['πŸ˜€', 'πŸ˜ƒ', 'πŸ˜„'].fill('πŸ€ͺ') = [ 'πŸ€ͺ', 'πŸ€ͺ', 'πŸ€ͺ' ]

// Map
['0️⃣', '1️⃣', '2️⃣', '3️⃣'].map((item, index) => item + " -> " + index) = [ '0️⃣ -> 0', '1️⃣ -> 1', '2️⃣ -> 2', '3️⃣ -> 3' ]

// Map
['0️⃣', '1️⃣', '2️⃣', '3️⃣'].filter((item, index) => index === 1) = [ '1️⃣' ]

//Reduce
['0️⃣', '1️⃣', '2️⃣', '3️⃣'].reduce((acc, current) => acc + current) = '0️⃣1️⃣2️⃣3️⃣'

//Push
['🀬', '😑', 'πŸ™‚', '😊'].push('πŸ˜„') = 5 // it will insert 'πŸ˜„' to list at last

//unshift
['😑', 'πŸ™‚', '😊', 'πŸ˜„'].unshift('🀬') = 5 // it will insert '🀬' to list at first

//Pop
['🀬', '😑', 'πŸ™‚', '😊', 'πŸ˜„'].pop('πŸ˜„') = 'πŸ˜„' // it will remove 'πŸ˜„' from list

//Shift
['🀬', '😑', 'πŸ™‚', '😊', 'πŸ˜„'].shift() = '🀬' // it will remove '🀬' from list

//Reverse
['🀬', '😑', 'πŸ™‚', '😊', 'πŸ˜„'].reverse() = [ 'πŸ˜„', '😊', 'πŸ™‚', '😑', '🀬' ]
Enter fullscreen mode Exit fullscreen mode

Thank you!!
Cheers!!

Top comments (5)

Collapse
 
vetrivel profile image
Vetrivel p

let arr = [0,1,2,3,4,5,6,7,8,9,10] - Input

let arr = [10,1,2,3,4,5,6,7,8,9,0] - output

Please explain how to rearrange.

Collapse
 
sakethkowtha profile image
sakethk • Edited
let arr = [0,1,2,3,4,5,6,7,8,9,10]

const swap = (list, x, y) =>  [ arr[x], arr[y] ] = [ arr[y], arr[x] ];


arr.sort((a, b) =>  a - b)
swap(arr, 0, arr.length - 1)
console.log(arr) //[10,1,2,3,4,5,6,7,8,9,0]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vetrivel profile image
Vetrivel p • Edited

Thank you so much!
I am beginner in javaScript, If possible can you expalin the code.

Thread Thread
 
sakethkowtha profile image
sakethk

We are sorting the array in ascending order after ascend ascending the array will be like this [0,1,2,3,4,5,6,7,8,9,10]. Later we need to swap first and last element in the array

Thread Thread
 
vetrivel profile image
Vetrivel p

ok, noted thank you so much.