当前位置:网站首页>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
- Ni visa fails after LabVIEW installs the third-party visa software
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
- Custom components, using NPM packages, global data sharing, subcontracting
- 网络安全-漏洞与木马
- How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
- Learn BeanShell before you dare to say you know JMeter
猜你喜欢
![[camera topic] turn a drive to light up the camera](/img/d3/7aabaa5c75813abc4a43820b4c3706.png)
[camera topic] turn a drive to light up the camera
![[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array](/img/2a/98400b0d0b5eda1e52abae30746f2b.jpg)
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array

【Camera专题】Camera dtsi 完全解析

How to deal with cache hot key in redis

深度(穿透)选择器 ::v-deep/deep/及 > > >

【Camera专题】手把手撸一份驱动 到 点亮Camera

详细些介绍如何通过MQTT协议和华为云物联网进行通信

"Jetpack - livedata parsing"

ByteDance data Lake integration practice based on Hudi

Rockchip3399 start auto load driver
随机推荐
Groovy, "try with resources" construction alternative
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
Asian Games countdown! AI target detection helps host the Asian Games!
Visual yolov5 format data set (labelme JSON file)
The testing process that software testers should know
Network security - virus
Redis:Redis的简单使用
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
Network security - scan
DML Foundation
网络安全-中间人攻击
NCTF 2018 part Title WP (1)
es6 filter() 数组过滤方法总结
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
[AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)
网络安全-扫描与密码爆破2
缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
Trial setup and use of idea GoLand development tool
详细些介绍如何通过MQTT协议和华为云物联网进行通信
Where is the future of test engineers? Confused to see