当前位置:网站首页>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)
边栏推荐
- 力扣(LeetCode)183. 从不订购的客户(2022.07.02)
- Where is the future of test engineers? Confused to see
- "Jetpack - livedata parsing"
- How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
- 全链路数字化转型下,零售企业如何打开第二增长曲线
- [camera special topic] Hal layer - brief analysis of addchannel and startchannel
- 技术大佬准备就绪,话题C位由你决定
- Network security - cracking system passwords
- 网络安全-防火墙
- Learn BeanShell before you dare to say you know JMeter
猜你喜欢
One of the C language practical projects is greedy snake
MySQL学习03
Ni visa fails after LabVIEW installs the third-party visa software
Why can't the start method be called repeatedly? But the run method can?
NCTF 2018 part Title WP (1)
【Camera专题】手把手撸一份驱动 到 点亮Camera
小程序開發的部分功能
Performance test | script template sorting, tool sorting and result analysis
[camera topic] how to save OTP data in user-defined nodes
stm32F407-------DMA
随机推荐
How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
Where is the future of test engineers? Confused to see
Custom components, using NPM packages, global data sharing, subcontracting
Take you ten days to easily complete the go micro service series (I)
Internal connection query and external connection
Network security - virus
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o
Asian Games countdown! AI target detection helps host the Asian Games!
Virtual list
[leetcode] 797 and 1189 (basis of graph theory)
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
ByteDance data Lake integration practice based on Hudi
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
STM32 - introduction of external interrupts exti and NVIC
Visual yolov5 format data set (labelme JSON file)
Button button adaptive size of wechat applet
网络安全-NAT网络地址转换
The technology boss is ready, and the topic of position C is up to you
MySQL学习03
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)