当前位置:网站首页>map和filter方法对于稀缺数组的处理
map和filter方法对于稀缺数组的处理
2022-06-26 17:23:00 【丹丹的小跟班】
稀缺数组
有一种数组叫稀缺数组,他的特点就是含有未赋值的索引。
比如:
let arr = [1]
arr.length = 10
这样一个数组长度为10,但是,除了第一位为1之外,其余的都是空,也就是使用了empty 字段占位。而map和filter作为最常用的数组方法之一,对于这种稀缺数组的处理是怎样的呢?
filter
任何问题首先看官方文档。
filter 为数组中的每个元素调用一次 callback 函数,并利用所有使得 callback 返回 true 或等价于 true 的值的元素创建一个新数组。callback 只会在已经赋值的索引上被调用,
对于那些已经被删除或者从未被赋值的索引不会被调用。那些没有通过 callback 测试的元素会被跳过,不会被包含在新数组中。
官方文档清晰的表示了filter对于稀缺数组的处理。那就是不会对稀缺数组里面的空值进行回调函数的调用,并且最终结果会自动排除空值,这其实可以算一个很好的在数组里面排除空值的方法。
map
我们只要注意官方文档的这样一句话:根据规范中定义的算法,如果被map调用的数组是离散的,新数组将也是离散的保持相同的索引为空。有人可能对离散表示疑惑,我们可以简单理解为就是稀缺数组,数组里面存在‘孔’,也就是空值(不是空字符串)。
var array = [1,,,2]
var mapArray = array.map((item, index) => {
console.log(index) //0, 3
return item + 1;
})
// 方法执行是打印 0,3 似乎跳过了数组中 hole 元素
console.log(mapArray) //[2, empty × 2, 3]
经测试发现map方法是将空值直接跳过不进行运算(这里跟filter一直),但是会在最终结果保留空值(这里跟filter又不一致)。
补充
当然除了filter和map之外,还有一些数组方法也会遇到这些情况,这里分享一篇看到的文章,文章里还介绍了findIndex,find ,forEach ,reduce 这些方法对于稀缺数组的处理。
文章链接
边栏推荐
- Platform management background and merchant menu resource management: merchant registration management design
- Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
- Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
- Byte interview: two array interview questions, please accept
- Microservice architecture practice: business management background and SSO design: SSO design
- Leetcode topic [array] -283- move zero
- Detailed explanation of browser storage methods: the origin and difference of cookies, localstorage and sessionstorage
- Basic requirements: 7 problems in singleton mode
- 类型多样的石膏PBR多通道贴图素材,速来收藏!
- 在国金证券开户怎么样?保障安全吗?
猜你喜欢

【推荐系统学习】推荐系统架构

vue--vuerouter缓存路由组件

LeetCode——226. 翻轉二叉樹(BFS)

有依赖的背包问题

牛客网:设计LRU缓存结构 设计LFU缓存结构

She said she was tired! So tired! I want to change my career

MySQL add column failed because there was data before, not null by default

玩转Linux,轻松安装配置MySQL

Army chat -- registration of Registration Center

Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
随机推荐
Army chat -- registration of Registration Center
接水面试题
Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
Teach you to learn dapr - 4 Service invocation
【代码随想录-动态规划】T583、两个字符串的删除操作
Leetcode topic [array] -283- move zero
Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
分布式架构概述
Some explanations for latex CJK
合约量化系统开发方案详细,量化合约系统开发技术说明
She said she was tired! So tired! I want to change my career
MySQL exports all table indexes in the database
Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
9、智慧交通项目(2)
【推荐系统学习】推荐系统架构
Turtle cartography
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
Don't believe it, 98% of programmers are like this
Web3 decentralized storage ecological landscape
玩轉Linux,輕松安裝配置MySQL