Common methods of JavaScript arrays for front-end developers

1. Modify the array

  • array.pop
    removes the last element of the array.
var arr = [1, 2, 3];
arr.pop();// 返回 3
arr;// [1,2]
  • array.shift
    removes the first element of the array
var arr = [1, 2, 3];
arr.shift();// 返回 1
arr;// [2,3]
  • array.push
    adds one or more elements to the end of the array.
var arr = [];arr.push(1);// 返回数组长度 1
arr;// [1]
arr.push(2,3);
arr;// [1,2,3]
  • array.unshift
    adds one or more elements to the beginning of the array.
var arr = [1, 2, 3];
arr.unshift(0);
arr.unshift(-3, -4);
arr;// [-3, -4, 0, 1, 2]
  • array.reverse reverses
    the order of array elements.
var arr = [1, 2, 3];
arr.reverse();// [3, 2, 1]
arr;// [3, 2, 1]
  • array.sort sorts
    an array.
var arr = [1 ,-1, 2];
arr.sort();// [-1, 1, 2]
arr;// [-1, 1, 2]
arr = [{age: 10,},{age: 1}, {age: 12}];// 按照 age 从小到大排序
arr.sort(function(a, b){return a.age - b.age > 0 ? 1 : -1;});
  • array.splice
    adds or removes elements to an array.
// splice(开始下标, 删除个数,插入元素(可以多个))var arr = [1, 2, 3, 4];
arr.splice(1, 2);// [2,3]
arr;// [1,4]
arr = [1, 2, 3, 4];
arr.splice(1, 1,'A','B','C');// [2,3]
arr;// [1, 2,"A","B","C", 4]

Note: When the array executes the above methods, the original array will be modified.

2. Iterative method

  • array.forEach
    iterates over the array.
['a' ,'b' ,'c'].forEach(function(each, index){console.log(each,index);});
// 输出 'a' 0  'b' 1 'c' 2
  • array.filter
    finds all elements from the array that match the specified criteria.
// 找出所有正数
var res = [3, 4, -1].filter(function(each){return each > 0;});
res; //[3,4]
  • array.every
    Whether each element in the array satisfies the specified condition.
// 是否都为正数
var isAllPositive = [3, 4, -1].every(function(each){return each > 0;});
isAllPositive; // false;
isAllPositive = [3, 4].every(function(each){return each > 0;});
isAllPositive; // true;
  • Whether any elements in the array.some
    array satisfy the specified condition.
// 是否有正数
var isSomePositive = [3, 4, -1].some(function(each){return each > 0;});
isSomePositive; // true;
isSomePositive = [-3, -4].every(function(each){return each > 0;});
isSomePositive; // false;
  • array.map
    maps an array into another array.
// 内容 * 2[1, 2, 3].map(function(each){return each * 2;});
// 返回 [2, 4, 6]
  • array.reduce
    combines an array into a single value.
// 数组内容求和。0 为初始值[1, 2, 3].reduce(function(prev, each){return prev + each;}, 0);// 返回 6

When iterative methods are to be used, forEach should be the last consideration. The main reason is: forEach has the worst semantics compared to other iteration methods. See avoid forEach for a more detailed description.

3 Other methods

  • Whether Array.isArray
    is an array. IE9+ supports this method.
Array.isArray(1); // false
Array.isArray({}); // false
Array.isArray([]); // true
  • array.concat concatenates
    an array or concatenates the values ​​of an array.
[1,2,3].concat(4,5); // 输出 [1, 2, 3, 4, 5]
  • array.join
    merges all elements of an array into a string.
[1,2,3].join(); // 输出 '1,2,3'
[1,2,3].join('.'); // 输出 '1.2.3'
  • array.slice
    selects a subset of elements in an array.
// slice(开始下标, 结束下标(可选,默认为数组长度))
['a', 'b', 'c', 'd'].slice(1);// ["b", "c", "d"]
['a', 'b', 'c', 'd'].slice(1, 2);// ["b"]
['a', 'b', 'c', 'd'].slice(1, 3);// ["b", "c"]
  • array.indexOf
    finds the index of the specified element in the array.
['a', 'b', 'c', 'd'].indexOf('c'); // 2
['a', 'b', 'c', 'd'].indexOf('g'); // -1
  • array.lastIndexOf
    finds the index of the specified element in the array. The search direction is from back to front.
['c', 'd', 'c'].lastIndexOf('c'); // 2
['a', 'b', 'c', 'd'].lastIndexOf('g'); // -1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860307&siteId=291194637