JavaScript

.push(), .concat(), .unshift(), .splice, .shift(), .pop(), .reverse()

Naranjito 2021. 1. 7. 12:51
  • .push() : push some informatino into the array, a commend adds a single value on the tail
  • .concat() : add more than a single value on the tail
  • .unshift() : add value on the start point into the array
  • .splice(index, howmany, element1, ..., elementN) : add value in somewhere in the middle of array.  It changes the contents of an array by removing or replacing existing elements and/or adding new elements in place
> var a=['a','b','c']
< undefined

> a.splice(1, 'ddd'); //add 'ddd' at first index
< [] (0)

> a
< ["a", "ddd", "b", "c"] (4)

> a.splice(1,1,"eee","fff"); //add "eee","fff" at first index in place and remove first index 
< ["ddd"] (1) //returns which element removed

> a
< ["a", "eee", "fff", "b", "c"] (5)
  • .shift() : remove the first element of array
  • pop() : Remove the item at the given index from the list and returns the removed item.
l=['python','java','c++']
l.pop(1)
>>>
'java'

l
>>>
['python', 'c++']
  • .reverse() : sorting reverse order