当前位置:网站首页>es6 filter() 数组过滤方法总结
es6 filter() 数组过滤方法总结
2022-07-03 01:32:00 【蓝胖子的多啦A梦】
es6 filter() 数组过滤方法总结
1.创建一个数组,判断数组中是否存在某个值
var newarr = [
{
num: 1, val: 'ceshi', flag: 'aa' },
{
num: 2, val: 'ceshi2', flag: 'aa2' }
]
console.log(newarr.filter(item => item.num===2 ))
2.也可以通过上面方法过滤掉num为2的留下num为1的
var newarr = [
{
num: 1, val: 'ceshi', flag: 'aa' },
{
num: 2, val: 'ceshi2', flag: 'aa2' }
]
console.log(newarr.filter(item => item.num!=2 ))
3.去掉空数组空字符串、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)
>//空字符串里面不能包含空格
var arr = ['1','2','', '3.jpg','']
var newArr = arr.filter(item => item)
console.log(newArr)
4.去掉数组中不符合项
var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40)
console.log(newArr)
5.过滤不符合项
var arr = ['10','12','23','44','42']
var newArr = arr.filter(item => item.indexOf('2')<0)
console.log(newArr)
6.数组去重
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.数组去重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.数组对象去重es6
let person = [
{
id: 0, name: "小明"},
{
id: 1, name: "小张"},
{
id: 2, name: "小李"},
{
id: 3, name: "小孙"},
{
id: 1, name: "小周"},
{
id: 2, name: "小陈"},
];
let obj = {
};
let peon = person.reduce((cur,next) => {
obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
return cur;
},[]) //设置cur默认类型为数组,并且初始值为空的数组
console.log(peon);
// (4) [{…}, {…}, {…}, {…}]0: {id: 0, name: "小明"}1: {id: 1, name: "小张"}2: {id: 2, name: "小李"}3: {id: 3, name: "小孙"}length: 4__proto__: Array(0)
边栏推荐
- 转载收录6.5大侠写的部分Qt开发经验
- word插入公式/endnote
- A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
- Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
- 小程序开发的部分功能
- Network security - DNS spoofing and phishing websites
- [data mining] task 3: decision tree classification
- Storage basic operation
- How is the mask effect achieved in the LPL ban/pick selection stage?
- PS remove watermark details
猜你喜欢

PS remove watermark details

Smart management of Green Cities: Digital twin underground integrated pipe gallery platform

Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)

Tâche 6: regroupement DBSCAN
![[data mining] task 2: mimic-iii data processing of medical database](/img/ad/4e7b253d60b29351e3ef252ee5230f.png)
[data mining] task 2: mimic-iii data processing of medical database
![[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

Rockchip3399 start auto load driver

网络安全-漏洞与木马

Huakaiyun (Zhiyin) | virtual host: what is a virtual host

His experience in choosing a startup company or a big Internet company may give you some inspiration
随机推荐
[data mining] task 4:20newsgroups clustering
Function definition and call, this, strict mode, higher-order function, closure, recursion
mysql
Network security - virus
小程序开发的部分功能
[shutter] animation animation (animatedbuilder animation use process | create animation controller | create animation | create components for animation | associate animation with components | animatio
[keil5 debugging] debug is stuck in reset_ Handler solution
2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
小程序開發的部分功能
Visual yolov5 format data set (labelme JSON file)
网络安全-防火墙
深度(穿透)选择器 ::v-deep/deep/及 > > >
[error record] the shutter component reports an error (no directionality widget found. | richtext widgets require a directionality)
Vant 实现简单的登录注册模块以及个人用户中心
Network security - password cracking
[camera topic] how to save OTP data in user-defined nodes
Analysis, use and extension of open source API gateway apisex
技术大佬准备就绪,话题C位由你决定
Niuniu's ball guessing game (dynamic planning + prefix influence)
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day