Skip to main content

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

The filter() method will not detect empty arrays and will not change the contents of the original array. The first parameter of this method is a callback function, and the second parameter is the object to be used as the callback to be executed. It is passed to the function and used as the value of "this". If thisValue is omitted, the value of "this" is "undefined", that is, if thisValue is omitted in strict mode, "this" is undefined; if thisValue is omitted in non-strict mode, "this" is window; if it is not omitted , "this" is the incoming object.

The following are some practical demonstration cases: The first is the basic use of the method.

       let arr=[1,2,3,4,5,6,7,8];
        let obj={
            id:'1001',
            name:"aaa",
        }
        let a= arr.filter(function(value,index,array){
            /* "use strict"
            console.log(this)//undefined */
            console.log(this);
            console.log(value,index,array);
            return value>2;
        },obj);
        console.log(a,Object.prototype.toString.call(a),a.length);
        console.log(arr);

Array deduplication

 let arr=[1,2,1,2,4,5];
        let a=arr.filter((value,index,array)=>{
            // return index===arr.indexOf(value);
            return index===array.indexOf(value);
        });
        console.log(a);// [1, 2, 4, 5]

Use with other methods

let arr=[1,2,3,4,5,6];
        let fun=function(num){
            if(num%2===0){
                return true;
            }
        }
        let a=arr.filter(fun);
        console.log(a);

Remove null items from an array

 let arr=[1,2,"null",null,"aaa",null];
        let a=arr.filter((value)=>{
            return value;
        })
        console.log(a)// [1, 2, "null", "aaa"]
        console.log(a)

Comments

Popular posts from this blog

Destructuring assignment in ES6

 In the past, when we wanted to assign a value to a variable, such as an array type and an object type, to assign a value to a variable, we could only specify the value directly. It is very troublesome to write in this way, but under the ES6 syntax specification, it is allowed to directly extract the required values from arrays and objects according to a certain pattern, and directly assign variables to variables. This method is called Destructuring, which is simple to understand. That is, the left and right sides of the equal sign are equal.

Using ref to bind elements in Vue

 Vue provides us with a property to manipulate dom, ref. When bound to the dom element, it is used similar to the id. It is called through this.$refs, and it can also be bound to the component. It is very troublesome to obtain DOM elements in the previous native way. It needs to be obtained with such a long AP as: documentgetElementXXX/querySXXX.... Then jQuery was born to solve this pain point. The use of $("id") makes Our operation has become easier. But there are other problems, jQuery is just a library (plug-in) and cannot be used to build a framework. Now Vue can help us solve this problem. Vue also has its own method to obtain DOM, which is ref. It can get not only DOM elements but also components. If you add ref to the dom element, you can get the dom element, and if you add it to the component, you can get the component instance, and you can use the component method.

mapState, mapGetters, mapMutations, mapActions in vuex

 The above four objects are all in the vuex plug-in store, which is often used to store some data shared globally. But I still feel a little uncomfortable when using it, that is, I wrote a lot of repeated prefixes. At this time, these four objects appeared, which solved this pain point very well. mapState is mainly used to manipulate and obtain data, mapGetters is equivalent to computed properties, and mapMutations and mapActions are simplifications of mutation and action. They can be written as both objects and arrays.