当前位置:网站首页>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)
边栏推荐
- stm32F407-------IIC通讯协议
- 网络安全-DNS欺骗与钓鱼网站
- 网络安全-openvas
- Asian Games countdown! AI target detection helps host the Asian Games!
- Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
- [shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
- What are the key points often asked in the redis interview
- Socket编程
- Introduction to kotlin collaboration
- [camera topic] turn a drive to light up the camera
猜你喜欢

网络安全-漏洞与木马

Virtual list

Network security - vulnerabilities and Trojans

Vant implements a simple login registration module and a personal user center

One of the C language practical projects is greedy snake
![[camera topic] turn a drive to light up the camera](/img/d3/7aabaa5c75813abc4a43820b4c3706.png)
[camera topic] turn a drive to light up the camera

Deep learning notes (constantly updating...)

How do it students find short-term internships? Which is better, short-term internship or long-term internship?

Redis:Redis的简单使用

Take you ten days to easily complete the go micro service series (II)
随机推荐
网络安全-浅谈安全威胁
Storage basic operation
网络安全-openvas
When the epidemic comes, how to manage the team as a leader| Community essay solicitation
Query product cases - page rendering data
Technology sharing | Frida's powerful ability to realize hook functions
网络安全-ACL访问控制列表
How can retail enterprises open the second growth curve under the full link digital transformation
Take you ten days to easily complete the go micro service series (I)
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
力扣(LeetCode)183. 从不订购的客户(2022.07.02)
去除网页滚动条方法以及内外边距
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
What are the key points often asked in the redis interview
Answers to ten questions about automated testing software testers must see
Leetcode(540)——有序数组中的单一元素
函数的定义和调用、this、严格模式、高阶函数、闭包、递归
His experience in choosing a startup company or a big Internet company may give you some inspiration
[error record] navigator operation requested with a context that does not include a naviga
网络安全-扫描