当前位置:网站首页>Summary of ES6 filter() array filtering methods
Summary of ES6 filter() array filtering methods
2022-07-03 02:01:00 【Blue fatty's Dora A Dream】
es6 filter() Summary of array filtering methods
1. Create an array , Determine whether there is a value in the array
var newarr = [
{
num: 1, val: 'ceshi', flag: 'aa' },
{
num: 2, val: 'ceshi2', flag: 'aa2' }
]
console.log(newarr.filter(item => item.num===2 ))
2. It can also be filtered out by the above method num by 2 Of them num by 1 Of
var newarr = [
{
num: 1, val: 'ceshi', flag: 'aa' },
{
num: 2, val: 'ceshi2', flag: 'aa2' }
]
console.log(newarr.filter(item => item.num!=2 ))
3. Remove the empty array, empty string 、undefined、null
var arr = ['1','2',undefined, '3.jpg',undefined]
var newArr = arr.filter(item => item)
console.log(newArr)
var arr = ['1','2',null, '3.jpg',null]
var newArr = arr.filter(item => item)
console.log(newArr)
>// Empty string cannot contain spaces
var arr = ['1','2','', '3.jpg','']
var newArr = arr.filter(item => item)
console.log(newArr)
4. Remove non conformances from the array
var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40)
console.log(newArr)
5. Filter nonconformities
var arr = ['10','12','23','44','42']
var newArr = arr.filter(item => item.indexOf('2')<0)
console.log(newArr)
6. Array weight removal
var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];
var arr2 = arr.filter((x, index,self)=>self.indexOf(x)===index)
console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]
7. Array weight removal es6
var arr=[1,2,1,'1',null,null,undefined,undefined,NaN,NaN]
let res=Array.from(new Set(arr));//{1,2,"1",null,undefined,NaN}
//or
let newarr=[...new Set(arr)]
8. Array object de duplication es6
let person = [
{
id: 0, name: " Xiao Ming "},
{
id: 1, name: " Xiao Zhang "},
{
id: 2, name: " petty thief "},
{
id: 3, name: " Little grandson "},
{
id: 1, name: " Xiaozhou "},
{
id: 2, name: " Xiao Chen "},
];
let obj = {
};
let peon = person.reduce((cur,next) => {
obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
return cur;
},[]) // Set up cur The default type is array , And an array with an empty initial value
console.log(peon);
// (4) [{…}, {…}, {…}, {…}]0: {id: 0, name: " Xiao Ming "}1: {id: 1, name: " Xiao Zhang "}2: {id: 2, name: " petty thief "}3: {id: 3, name: " Little grandson "}length: 4__proto__: Array(0)
边栏推荐
- mysql
- 浏览器是如何对页面进行渲染的呢?
- File class (check)
- Machine learning notes (constantly updating...)
- Trial setup and use of idea GoLand development tool
- [data mining] task 2: mimic-iii data processing of medical database
- 网络安全-扫描
- 网络安全-动态路由协议RIP
- Network security - dynamic routing protocol rip
- 【Camera专题】手把手撸一份驱动 到 点亮Camera
猜你喜欢
MySQL学习03
mysql
[leetcode] 797 and 1189 (basis of graph theory)
Performance test | script template sorting, tool sorting and result analysis
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
[data mining] task 3: decision tree classification
小程序開發的部分功能
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
可视化yolov5格式数据集(labelme json文件)
Redis: simple use of redis
随机推荐
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
Network security ACL access control list
网络安全-扫描
stm32F407-------ADC
leetcode961. Find the elements repeated N times in the array with length 2n
缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
Asian Games countdown! AI target detection helps host the Asian Games!
STM32 - introduction of external interrupts exti and NVIC
"Jetpack - livedata parsing"
[Appendix 6 Application of reflection] Application of reflection: dynamic agent
[data mining] task 3: decision tree classification
Analyzing several common string library functions in C language
Virtual list
Visual yolov5 format data set (labelme JSON file)
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
可視化yolov5格式數據集(labelme json文件)
[leetcode] 797 and 1189 (basis of graph theory)
Storage basic operation
DDL basic operation
His experience in choosing a startup company or a big Internet company may give you some inspiration