Home  >  Article  >  Web Front-end  >  js array operation recording method

js array operation recording method

一个新手
一个新手Original
2017-10-12 09:38:261709browse

一.splice()

Method adds/removes items to/from the array and returns the deleted item.

 arrayObject.splice(index,howmany,item1,...,itemX)

Parameter Description
index Required. An integer specifying the position at which to add/remove an item. Use a negative number to specify the position from the end of the array.
howmany Required. The number of items to delete. If set to 0, items will not be deleted.
item1, ..., itemX Optional. New items added to the array.

Example:


let a=[1,2,3];
a.splice(1,1,666);
console.log(a);//[1,666,3]

2.slice()

The slice() method returns selected elements from an existing array.

 arrayObject.slice(start1,end2)

start1 Required. Specifies where to start the selection. If negative, it specifies the position from the end of the array. That is, -1 refers to the last element, -2 refers to the second to last element, and so on. end2 Optional. Specifies where the selection ends. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to the end of the array. If this parameter is negative, it specifies the elements starting from the end of the array.
##Parameters Description

Example:

let a=[1,2,3,4,5];
console.log(a.slice(1,4)); //[2,3,4]

3.concat()

Method is used to connect two or more arrays.

This method does not change the existing array, but only returns a copy of the connected array.

Return a new array. The array is generated by adding all arrayX parameters to arrayObject. If the argument to concat() is an array, then the elements in the array are added, not the array.

 

arrayObject.concat(X,X,......,X)

XRequired. This parameter can be a specific value or an array object. Can be any number.
Parameters Description

Example:

  let a=[1,2,3];
  let b=[4,5,6];
  console.log(a.concat(b));
//[1,2,3,4,5,6]
  console.log(a.concat(4,5,6));
//[1,2,3,4,5,6]

Four.sort()

Method is used to sort the elements of the array.

 

arrayObject.sort(sortby)

Optional. Specifies the sort order. Must be a function.
Parameter Description
sortby

sortbyThe function should have two parameters a and b, and its return value is as follows:

  • If a is less than b, a should appear before b in the sorted array, then a value less than 0 is returned.

  • If a is equal to b, return 0.

  • If a is greater than b, return a value greater than 0.

Example:

let a = [2, 4, 1, 3];
  console.log(a.sort(function(a, b) {
    return a-b;  })); //[1,2,3,4]

5. for of

 1. The new function in es6 is used to traverse the array reference: Iterator and for...of loop

 2. The difference between for in and for of:

   Simply put, for in traverses key names, and for of traverses key values.

let arr = ["a","b"];for (a in arr) {
    console.log(a);//1,2}for (a of arr) {
    console.log(a);//a,b}

Because of this feature of for of, it can also traverse the iterator object, while for in is a simple traversal.

5. Finally

Today I will collect the basic knowledge of js arrays. Used to consolidate one's own knowledge regularly.

The above is the detailed content of js array operation recording method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn