js array method

Keywords: Javascript Front-end

1.join()

Receives a parameter as a delimiter, defaults to a comma, returns a string separated by each item of the array

var arr = [1,2,3,]
  console.log(arr.join(".")) //1.2.3

2.split()

Receives one or two parameters, the first representing a "separator" or a "regular expression" separated from the position specified by the parameter, and the second optional representing the length of the separation;

Note: If an empty string ('') is used as a separator, then each character in the stringObject is split and the original string remains unchanged.

Returns an array of strings

var str = "javascript"
var str1 = "j/a/v/a/s/c/r/i/p/t"
   console.log(str.split('',5)) //['j', 'a', 'v', 'a', 's']
   console.log(str1.split("/")) //['j', 'a', 'v', 'a', 's', 'c', 'r', 'i', 'p', 't']

String.split() does the opposite of what Array.join does.

3.splice()

Delete replaces and inserts.

Delete: Receives two parameters, the location of the first item to delete and the number of items to delete

Insert: Receives three or more parameters, the position of the first item to be deleted, the number of deleted items 0, the value inserted

Substitution: Receives three or more parameters, the location of the first item to be deleted, the number of items deleted, the value to be replaced

Returns an array containing deleted items

 var arr = [1,2,3,4,5,6]
    console.log(arr.splice(2,1)) //[3]
    console.log(arr) //[1, 2, 4, 5, 6]
    console.log(arr.splice(1,3,45,6)) //[2, 4, 5]
    console.log(arr) //[1, 45, 6, 6]

4.slice()

You can accept one or two parameters, with only one parameter, and return all items from the start subscript to the end. If there are two parameters, items from the start to the end do not include items from the end

Returns an array of each component of a set of partitions

var arr = [1,2,3,4,5,6]
   console.log(arr.slice(2)) //[3, 4, 5, 6]
   console.log(arr.slice(1,3)) //[2, 3]

5.push() and pop()

push() accepts any arguments and adds them one by one to the end of the array, changing the original array and returning the length of the array

pop() has no parameters and even returns removes the last item of the array, changes the original array, and returns the value of the item

   var arr = [1,2,3,4,5,6]
     console.log(arr.push(8,9,10)) //9
     console.log(arr.pop()) //10
     console.log(arr) //[1, 2, 3, 4, 5, 6, 8, 9]

6.unshift() and shif()

unshift() accepts any arguments and adds them one by one to the beginning of the array, alters the original array, and returns the length of the array

shift() has no parameters and even returns removes the first item of the array, changes the original array, and returns the value of the item

  var arr = [1,2,3,4,5,6]
     console.log(arr.unshift(8,9,10)) //9
     console.log(arr.shift()) //8
     console.log(arr) //[9, 10, 1, 2, 3, 4, 5, 6]

7.concat()

You can receive multiple parameters, which can be arrays, which combine each item in the array with the original and return a new array

    var arr = [1,2,3,4,5,6]
       console.log(arr.concat(4,[5,6,66,6])) //[1, 2, 3, 4, 5, 6, 4, 5, 6, 66, 6]
       console.log(arr) //[1, 2, 3, 4, 5, 6]

8.sort() 

Bubble sort, accepts a comparison function as a parameter, which has two parameters, returns the result of a parameter minus b parameter, and returns a new array

    var arr = [6,2,5,4,8,10,5,6]
       console.log(arr.sort((a,b)=>a-b)) //[2, 4, 5, 5, 6, 6, 8, 10]

9.reverse()

Flip each item in the array without parameters

   var arr = [6,2,5,4,8,10,5,6]
      console.log(arr.reverse()) //[6, 5, 10, 8, 4, 5, 2, 6]

10.indexOf() and lastIndexOf()

indexOf(): Receives two parameters, the item to be searched for and the index (optional) to find the starting position of the index (optional) from the beginning of the array (position 0) looking backwards.
lastIndexOf(): Receives two parameters, the item to find and the index (optional) to find the starting position, looking forward from the end of the array.

Returns the index of the first occurrence of the item, or -1 if it is not found

   var arr = [6,2,5,4,8,10,5,6]
    console.log(arr.indexOf(5)) //2
    console.log(arr.indexOf(5,4))  //6
    console.log(arr.lastIndexOf(5)) //6

Posted by sanstenarios on Mon, 20 Sep 2021 20:32:16 -0700