Simple array usage finishing

map

map () method returns a new array, the array element calls a function of the value of the original processing array elements.

map () method in the original order of the processing elements in the array elements.

Note: map () will not be an empty array detection.

Note: map () does not alter the original array.

Array.find()

find () method returns the value of the first array element by testing functions.

This example finds (returns) the first element is greater than 18 (the value):

var numbers = [4, 9, 16, 25, 29];
var first = numbers.find(myFunction);

document.getElementById ( "demo") innerHTML = "the first value is greater than 18:". + first;

function myFunction(value,) {
return value > 18;
}

filter

filter () method creates a new array of new elements in the array by checking the specified array qualifying all the elements.

Note:  filter () will not be an empty array detection.

Note:  filter () does not alter the original array.

How to use the join () method all the elements of the array a string .

join () method all array elements can also be incorporated into a string.

Its behavior is similar to toString (), but you can also specify delimiters:

var arr = new Array(3);
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr.join());

document.write("<br />");

document.write(arr.join("."));

合并数组concat
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

var arr2 = new Array(3)
arr2[0] = "James"
arr2[1] = "Adrew"
arr2[2] = "Martin"

document.write(arr.concat(arr2))

Array.prototype.includes()方法

Includes () method is used to determine whether an array contains a specified value, in some cases, if it contains returns true, otherwise returns false.

var array = [1, 2, 3];

console.log(array.includes(2)); // expected output: true var pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // expected output: true console.log(pets.includes('at')); // expected output: false

Array.prototype.includes () method takes two parameters: Similar indexOf

  • Value to search for
  • Starting index of the search.

When the second parameter is transferred in, the method will start from the next search index (index of the default value is 0). It returns true if the search value exists in an array, otherwise false. Let us look at the following example:

['a', 'b', 'c', 'd'].includes('b') // true ['a', 'b', 'c', 'd'].includes('b', 1) // true ['a', 'b', 'c', 'd'].includes('b', 2) // false 

At first glance, the action includes an array of overlapping with indexOf, why should such a deliberately increase the api it? The main differences are the following:

  • return value. Look a function, look at their return value. Returns the number of indexOf is a value type, includes the return value is a Boolean type, includes so much simpler if the conditions at the time of judgment, and indexOf need to write a condition to judge.

    splice () method can be used to add a new item to the array:

    Examples

    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    fruits.splice(2, 0, "Lemon", "Kiwi");
    

     

    The first parameter (2) defines the location (splicing) the new element should be added.

    The second parameter (0) define how many elements should be removed.

    The remaining parameters ( "Lemon", "Kiwi") to add a new element definitions.

    splice () method returns an array containing the deleted items:

isNaN () method

If the argument is  NaN, then the global  isNaN () method returns  true. Otherwise, it returns  false:

Examples

isNaN("Hello"); 

Popping 和 Pushing

In dealing with the array, remove elements and add new elements is very simple.

Popping and Pushing means:

From the array of pop project, or push the project to the array.

Popping

pop () method removes the last element from an array:

Examples

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();    

Pushing

push () method (at the end of the array) to add a new element to the array:

Examples

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");   

Promise.prototype.finally() -es9

This basic nothing to talk about, you can read the names to see. Its usage is as follows:

promise
  .then(result => {···})
  .catch(error => {···})
  .finally(() => {···});

Guess you like

Origin www.cnblogs.com/zfdbk/p/11357108.html