ES6 learning summary — new array knowledge in ES6

Time:2020-8-1

Array.from (xxx) method

let  json = {
    '0': 'aaa',
    '1': 'bbb',
    '2': 'ccc',
    length:3
}
let  json2 = {
    'name': 'aaa',
    'name1': 'bbb',
    'name2': 'ccc',
    length:3
}
console.log(Array.from(json));
console.log(Array.from(json2));
result:
["aaa", "bbb", "ccc"]
[undefined, undefined, undefined]

This is a standard JSON array format. Compared with ordinary JSON, a length attribute is added at the end. As long as it is this special JSON format, it can be easily transformed into an array using the syntax of ES6. In ES6, most array operations exist in array objects. We use it Array.from (xxx).

be careful:
use Array.from (xxx) when converting, you must note that the format of JSON must be array format [such as JSON above], but the format similar to json2 above will not work

Array.of () method

It is responsible for converting a pile of text or variables into an array. In the development, we often get a string similar to an array and need to use Eval to convert it. If you, an experienced programmer, know that Eval is inefficient, it will slow down our program. Then we can use it Array.of method. Let’s look at the code below to convert a pile of numbers or strings into arrays and print them on the console

let arr =Array.of(3,4,5,6);
console.log(arr);
let arr2 =Array.of('aaa','vvv','fff');
console.log(arr2);

result:

[3, 4, 5, 6]
['aaa', 'vvv', 'fff']

Find() instance method

The so-called instance method does not start with an array object, but must have an existing array, and then use the method. This is the instance method. The find method here is to find from an array. In the find method, we need to pass in an anonymous function with three parameters:

  • Value: represents the value of the current search.
  • Index: represents the array index of the current lookup.
  • Arr: represents the current array.

stayIf a qualified array element is found in the function, it will return and stop searching。 You can copy the code below to test, and you will know the function of find.

let arr=[1,2,3,4,5,6,7,8,9];
console.log(arr.find(function(value,index,arr){
    return value > 5;
}))
console.log(arr.find(function(value,index,arr){
    return value < 1;
}))
let a = arr.find((val,index,arr)=>{
    return arr
})
console.log(a);

result:

6
undefined
1

The console outputs 6, which indicates that a qualified value is found and returned. If it cannot be found, undefined will be displayed.

Fill() instance method

Fill() is also an instance method. Its function is toArrayIt receives three parameters. The first parameter is the variable to be filled, the second is the position where the filling starts [occupied by variable], and the third is the position filled in [not occupied by variables, still displayed]

let arr=[0,1,2,3,4,5,6,7,8,9];
arr.fill('aaa',2,5);
console.log(arr);

result:

[0, 1, "aaa", "aaa", "aaa", 5, 6, 7, 8, 9]

Traversal of array

for… Of cycle

let arr=['aaa','bbb','ccc']

for (let item of arr){
    console.log(item);
}

result:

 aaa
 bbb
 ccc

Recommended Today

ASP.NET Example of core MVC getting the parameters of the request

preface An HTTP request is a standard IO operation. The request is I, which is the input; the responsive o is the output. Any web development framework is actually doing these two things Accept the request and parse to get the parameters Render according to the parameters and output the response content So let’s learn […]