[Summary of filtering methods] JS simply implements multi-condition filtering of array objects, returns a new array, and filters another array object based on the elements in one array [continuous updates]

Multi-condition filter array object

Usage: filterConditions is the filter condition, and the key must be consistent with the key in the filtered array.
If the filter condition is not passed or passed empty, the original data will be returned without filtering. If there is value, it will be filtered.
The second parameter is the array object that needs to be filtered.
Note here that the judgment conditions for returning the original array can be set by yourself,
For example, I added a queryObj[key] == '0'
because I have all the options in the drop-down box options, and the value I got is 0.
All data needs to be returned, so I added one,
If there are other special needs options or values ​​that need to be returned to the original array, just add or delete them yourself
The main comparison is here p[key] == queryObj[key]
Here I directly compare whether they are consistent. If you need fuzzy query,
For example, if all items containing this value are filtered out, you can modify it here

let filterConditions = {
    
    
 channel: this.qd,
 month: this.time
}
 let datas= this.filterData(filterConditions, this.tableDataAll)

// 多条件过滤,参数queryObj(过滤条件对象),list(需要过滤的数组)
filterData(queryObj, list) {
    
    
  let arr = list
     Object.keys(queryObj).forEach(key => {
    
    
     if (queryObj[key] == undefined || queryObj[key] == '' || queryObj[key] == '0')return arr 
	 arr = arr.filter(p => p[key] === null || p[key] == queryObj[key])})
	 return arr
},

Filters an array object based on the elements in another array. Filter out a field in the array object if it exists in the array

kf is an array with multiple names in it. Filter the data with the same name in the array object and return it.

let result = this.tableData.filter((item)=>{
    
    
        if(this.kf.length==0){
    
    
      		return this.tableData
		}else{
    
    
        	return this.kf.some(curVal => (curVal === item.name)) 
    	}
})

The array object individually filters the data that meets the requirements and returns

let result = this.tableData.filter((item)=>{
    
    
   return item.part_id==50
})

Guess you like

Origin blog.csdn.net/seeeeeeeeeee/article/details/134077155