当前位置:网站首页>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)
边栏推荐
- 网络安全-openvas
- Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
- Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
- Cloud native topic sorting (to be updated)
- es6 filter() 数组过滤方法总结
- Introduction to kotlin collaboration
- [shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
- Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
- 网络安全-木马
- Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
猜你喜欢
stm32F407-------IIC通讯协议
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
Some functions of applet development
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
Vant implements a simple login registration module and a personal user center
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
[data mining] task 2: mimic-iii data processing of medical database
查询商品案例-页面渲染数据
随机推荐
Everything file search tool
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
Network security - scanning and password explosion 2
LabVIEW安装第三方VISA软件后NI VISA失效
Network security - cracking system passwords
[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
Redis:Redis的简单使用
【Camera专题】HAL层-addChannel和startChannel简析
Depth (penetration) selector:: v-deep/deep/ and > > >
网络安全-漏洞与木马
What are the key points often asked in the redis interview
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
stm32F407-------IIC通讯协议
Types of map key and object key
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
File class (check)
小程序开发黑马购物商城中遇到的问题
How do browsers render pages?
Network security ACL access control list
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)