This is the 14th day of my participation in the August Text Challenge.More challenges in August

JavaScript array API

As of the ES7 specification, the array contains a total of 33 standard API methods and one non-standard API method

Constructor for Array

Normally arrays are created using object literals (var a = []), but this is limited because you can’t create a fixed-length array this way.

// Use the Array constructor to customize the length
var a = Array(6); / / / the empty x 6
// Use object literals
var b = [];
b.length = 6; / / / undefined x 6
Copy the code

The Array constructor works in two ways, depending on the length of the argument

  • New Array (arg1, arg2,…). The arguments passed in will be the 0th to NTH entries of the new array in order (if the argument length is 0, the empty array is returned);
  • New Array(len) Returns an Array containing only one len element if len is not a number. When len is a number, it returns an empty array of the corresponding length.

ES6 new constructor

  1. Array.of

Array.of is used to convert arguments to one item in an Array and then return the new Array, regardless of whether the argument is numeric or otherwise. It basically functions the same as the Array constructor, the only difference being the handling of a single numeric parameter.

The Array constructor cannot create a single-digit Array directly. The Array of can

Array(2) / / / the empty x 2
Array.of(2) / / [2]
Copy the code
  1. Array.from

It is designed to create new arrays based on objects. As long as an object has an iterator, array. from can turn it into an Array.

Array.from takes three arguments: the first is mandatory and the last two are optional and must have a return value

  1. An array-like object, mandatory.
  2. Processing function, the newly generated array will be processed by the function and then returned;
  3. This refers to the value of this when the processing function is executed.
var obj = {0: 'a', 1: 'b', 2:'c', length: 3};
Array.from(obj, function(value, index){
  console.log(value, index, this, arguments.length);
  return value.repeat(3);   // A return value must be specified, otherwise undefined is returned
}, obj);
Copy the code

In addition to obj, objects with iterators include Strings, sets, maps, and array. from

An Array of judgment

var a = [];
// 1. Is there an Array on the instanceof a prototype chain
a instanceof Array;
// 2. The constructor based on constructor a is Array
a.constructor = = = Array;
/ / 3. Based on the Object. The prototype. IsPrototypeOf is to see if a on the prototype chain of an Array
Array.prototype.isPrototypeOf(a);
GetPrototypeOf getPrototype = a.__proto__
Object.getPrototypeOf(a) = = = Array.prototype;
/ / 5. Based on the Object. The prototype. ToString
Object.prototype.toString.apply(a) = = = '[object Array] ';Copy the code

A method that changes the array itself

There are nine of them

Pop | | push | | reverse | | shift | | sort | | splice | | unshift and two new added ES6 copyWithin and the fill

/ / pop method
var array = ["cat"."dog"."cow"."chicken"."mouse"];
var item = array.pop();
console.log(array); // ["cat", "dog", "cow", "chicken"]
console.log(item); // mouse
Copy the code

The array.pop() method removes the element at the end of the array and returns the deleted element

/ / push method
var array = ["football"."basketball"."badminton"];
var i = array.push("golfball");
console.log(array); // ["football", "basketball", "badminton", "golfball"]
console.log(i); / / 4
Copy the code

Array.push (< added object >) is used to add objects from the end of an array. The return value is the length of the current array

/ / reverse method
var array = [1.2.3.4.5];
var array2 = array.reverse();
console.log(array); / /,4,3,2,1 [5]
console.log(array2===array); // true
Copy the code

Array.reverse () changes the array array and returns the changed array

/ / shift method
var array = [1.2.3.4.5];
var item = array.shift();
console.log(array); / / 5-tetrafluorobenzoic [2]
console.log(item); / / 1
Copy the code

Array.shift () removes the array header element, returning the deleted element

/ / the unshift method
var array = ["red"."green"."blue"];
var length = array.unshift("yellow");
console.log(array); // ["yellow", "red", "green", "blue"]
console.log(length); / / 4
Copy the code

Array.unshift () removes the end element of the array and returns the length of the array

/ / sort method
var array = ["apple"."Boy"."Cat"."dog"];
var array2 = array.sort();
console.log(array); // ["Boy", "Cat", "apple", "dog"]
console.log(array2 = = array); // true
Copy the code

Array.sort () sorts an array and returns the sorted array

/ / splice method
var array = ["apple"."boy"];
var splices = array.splice(1.1);
console.log(array); // ["apple"]
console.log(splices); // ["boy"]
Copy the code

Array.splice (args) is used to cut arrays. The return value is the array cut by splice

/ / copyWithin method
var array = [1.2.3.4.5]; 
var array2 = array.copyWithin(0.3);
console.log(array= = =array2,array2);  // true [4, 5, 3, 4, 5]
Copy the code

The array.copywithin (0,3) method copies everything from index 3 to the end to index 0

/ / the fill method
var array = [1.2.3.4.5];
var array2 = array.fill(10.0.3);
console.log(array= = =array2,array2); 
// true [10, 10, 10, 4, 5], all elements in the range [0,3] are replaced by 10
Copy the code

Array.fill (< element >, < pre-interval >, < post-interval >) replaces the element of the corresponding interval with an element