当前位置:网站首页>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)
边栏推荐
- 浏览器是如何对页面进行渲染的呢?
- Swift开发学习
- 微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
- Take you ten days to easily complete the go micro service series (I)
- Asian Games countdown! AI target detection helps host the Asian Games!
- [shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
- Redis: simple use of redis
- 去除网页滚动条方法以及内外边距
- Groovy, "try with resources" construction alternative
- 【Camera专题】OTP数据如何保存在自定义节点中
猜你喜欢

深度学习笔记(持续更新中。。。)

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

Processing of tree structure data

What are the key points often asked in the redis interview
![[Appendix 6 Application of reflection] Application of reflection: dynamic agent](/img/e7/0ee42902b178b13e9a41385267e7b6.jpg)
[Appendix 6 Application of reflection] Application of reflection: dynamic agent

Machine learning notes (constantly updating...)

Network security - vulnerabilities and Trojans

Deep learning notes (constantly updating...)

Redis:Redis的简单使用

Ni visa fails after LabVIEW installs the third-party visa software
随机推荐
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o
stm32F407-------ADC
Prohibited package name
stm32F407-------DMA
Answers to ten questions about automated testing software testers must see
Network security - virus
MySQL learning 03
网络安全-防火墙
Query product cases - page rendering data
[data mining] task 2: mimic-iii data processing of medical database
Performance test | script template sorting, tool sorting and result analysis
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
详细些介绍如何通过MQTT协议和华为云物联网进行通信
[data mining] task 1: distance calculation
疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
Certaines fonctionnalités du développement d'applets
机器学习笔记(持续更新中。。。)
[Yu Yue education] Jiujiang University material analysis and testing technology reference
Analyzing several common string library functions in C language
小程序开发的部分功能