Filter

Using filter, the find method filters the elements of an array object by filtering criteria

Let arr = [{code: '1', name: 'apple'}, {code: '2', name: 'snow pear'}, {code: '3', name: 'grapes'}, {code:' 4 ', name: "banana"}, {code: '5', name: 'orange'}, ] // let searchCode = '3' let searchByFilter = arr.filter(item => item.code === '3') let searchByFind = arr.find(item =>  item.code === searchCode) console.log(searchTarget) // [{code: "3", name: [] console.log(searchByFind) // {code: "3", name: "grape "}, undefinedCopy the code

Two array objects take the common set

Let arr2 = [{code: '1', name: 'apple'}, {code: '4', name: 'snow pear'}, {code: '3', name: 'grapes'}, {code:' 9 ', the name: "banana"}, {code: '7', name: 'orange '},]// some is used to check whether the elements in the array satisfy the specified condition (provided by the function). let arr3 = arr.filter(item => arr2.some(it =>it.code === item.code))console.log(arr3);Copy the code

Map

The map() method creates a new array, and the result is the result returned when each element in the array calls one of the provided functions.

Here’s an example:

Var array1 =,4,9,16 [1]; const map1 = array1.map(x => x *2); console.log(map1);Copy the code

Print the result

> Array [2,8,18,32]

And when I write like this:

var array1 = [1, 4, 9, 16]; const map1 = array1.map(x => { if (x == 4) { return x * 2; }}); console.log(map1);Copy the code

The printed result is:

> Array [undefined, 8, undefined, undefined]
Copy the code

Why do we have three undefined’s? Instead of what I expected [1,8,9,16]. This only adds the condition that x is multiplied by 2 when the value of x is 4. Undefined is present because the map() method creates a new array, but the new array is not assigned after array1 is traversed, it gets a value every time. So, the following modification is correct:

var array1 = [1, 4, 9, 16];
 
const map1 = array1.map(x => {
    if (x == 4) {
        return x * 2;
    }
    return x;
});
Copy the code

The join function

Push and pop

The shift and the unshift

sort

Reverse function

Once met an interview, array splicing.

I then answered concat and used the loadash tool

Actually, the easiest way is to deconstruct it

<! DOCTYPE HTML > < HTML lang="en"> <head> <meta charset="UTF-8"> <title>ES6 destruct instead of concat array join </title> </head> <body> <script type="text/javascript"> let a = [10, 20, 30] let b = [40, 50] let c = [...a, Log (c) // 10 20 30 40 50 </script> </body> </ HTML > <script> var a = [1,2,30] var b = [5,6,7] // var c = a.concat(b) var c = [...a,...b] console.log(c); </script>Copy the code

slice 

Where negative numbers mean counting from the back

splice

IndexOf and lastindexOf

There is also an includes similar to indexOf, which belongs to ES7 and is not supported by some older browsers

some

every

De-duplication can also be done with set (new data deconstruction for ES6)

Because the key value is unique