An array of API

Reference MDN: developer.mozilla.org/zh-CN/docs/…

** array.isarray () ** Used to determine if the value passed is oneArray

Array.isArray([1, 2, 3]);  
// true
Array.isArray({foo: 123}); 
// false
Array.isArray("foobar");   
// false
Array.isArray(undefined);  
// false
Copy the code

arr.splice

Syntax: arr.splice (start, delete, add), the first parameter is mandatory developer.mozilla.org/zh-CN/docs/… 1) Integrate new arrays

let  arr = ['t'.'a'.'o'.'w'.'u']; Arr.splice (1)// Start to delete other ["t"] arr. Splice (1,1)"t"."o"."w"."u"]
arr.splice(arr.length,0,'t'.'w'.'h')// Start by removing 0 bits and add ["t"."a"."o"."w"."u"."t"."w"."h"]
Copy the code

2) String to array

function list() {
          returnArray.prototype.slice.call(arguments); } var list1 = list(1, 2, 3); / / [1, 2, 3]Copy the code

The first digit of the array adds data

let  arr = ['t'.'a'.'o'.'w'.'u'];
arr.unshift(0)
Copy the code

Or:

let  arr = ['t'.'a'.'o'.'w'.'u']; Arr. Splice (0, 0,"t")
Copy the code

First digit delete data

let  arr = ['t'.'a'.'o'.'w'.'u'];
arr.shift(0)
Copy the code

The last bit adds data

let  arr = ['t'.'a'.'o'.'w'.'u'];
arr.push(6)
Copy the code

The last bit deletes the data

let  arr = ['t'.'a'.'o'.'w'.'u'];
arr.pop()
Copy the code

Sort an array

The **sort()** method sorts the elements of an array using an in-place algorithm

    let arr = ['1'.'2'.'6'.'77'.'7'];
    arr.sort(function (a, b) {
      return a - b;
    })
Copy the code


An array of reverse

The **reverse()** method reverses the position of the elements in the array

const array1 = ['one'.'two'.'three'];
console.log('array1:', array1);
// expected output: "array1:" Array ["one"."two"."three"]
Copy the code





  • The pseudo-array object (has onelengthProperty and a number of index properties for any object)
  • Iterable (can retrieve elements of an object, such as maps and sets) array from ()

fromStringTo generate an array

Array.from('foo'); / / /"f"."o"."o" ]
Copy the code

fromSetTo generate an array

const set = new Set(['foo'.'bar'.'baz'.'foo']);
Array.from(set); / / /"foo"."bar"."baz" ]
Copy the code

fromMapTo generate an array

const map = new Map([[1, 2], [2, 4], [4, 8]]);
Array.from(map);
// [[1, 2], [2, 4], [4, 8]]
const mapper = new Map([['1'.'a'], ['2'.'b']]); Array.from(mapper.values()); / / /'a'.'b']; Array.from(mapper.keys()); / / /'1'.'2'];
Copy the code

Generates arrays from class array objects (arguments)

function f() {
  returnArray.from(arguments); } f(1, 2, 3); // [1, 2, 3]Copy the code

inArray.fromUse the arrow function in

// Using an arrow function as the map functionto // manipulate the elements Array.from([1, 2, 3], x => x + x); / / (2, 4, 6]Copy the code

Concat merges two or more arrays

const array1 = ['a'.'b'.'c'];
const array2 = ['d'.'e'.'f'];
const array3 = array1.concat(array2);
Copy the code

4) Deconstruct assignment

5) an array of ()

String to array

function list() {
          returnArray.prototype.slice.call(arguments); } var list1 = list(1, 2, 3); / / [1, 2, 3]Copy the code

Array to string

**toString()** returns a string representing the specified array and its elements

const array1 = [1, 2, 'a'.'1a'];
console.log(array1.toString());
console.log(array1.join());
Copy the code

join

const elements = ['Fire'.'Air'.'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(' '));
// expected output: "FireAirWater"

console.log(elements.join(The '-'));
// expected output: "Fire-Air-Water"
Copy the code

Find if the array contains a value

The ****includes()** **includes()** method is used to determine whether an array contains a specified value, and returns true if it does

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true
Copy the code

Method 2 **indexOf()** **indexOf()** returns the first indexOf a given element that can be found in the array, or -1 if none exists

const beasts = ['ant'.'bison'.'camel'.'duck'.'bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

console.log(beasts.indexOf('giraffe'));
// expected output: -1
Copy the code

Get each data in the array and do something else (no return value)

Foreach is a common procedure associated with retrieving data. Foreach () changes the value of the original array

    <script>
        const array = ['a'.'b'.'c'];
        array.forEach((item, index) => {
            console.log(item)
        });
    </script>
Copy the code

Do not add data by deleting in foreach.

Filter array

**filter()** creates a new array. Return is followed by a condition, which returns a new array if true

const words = ['spray'.'limit'.'elite'.'exuberant'.'destruction'.'present'];

const result = words.filter(item => item.length > 6);

console.log(result);
// expected output: Array ["exuberant"."destruction"."present"]
Copy the code

Iterate to get a new array (with return value, one-to-one)

The new array returned by traversal does not change the original array, so it is recommended to use it during traversal

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
Copy the code

Sum or average

Reduce arr. Reduce (sum, currentData, index)

let arr =[0, 1, 2, 3, 4];
arr.reduce((accumulator, currentValue, currentIndex, array)=>{
  return accumulator + currentValue;
});
Copy the code