Map和splice给出空而不是空(Map and splice gives empty instead of nothing)

编程入门 行业动态 更新时间:2024-10-17 21:19:16
Map和splice给出空而不是空(Map and splice gives empty instead of nothing)

我有一个算法:

let someArray = [1, 2, 3, 4, 5]
let mapped = someArray.map(number => {
    let index = someArray.indexOf(5)
    if (index !== -1) {
        someArray.splice(index, 1)
    }
    console.log(typeof number)
    return number
})
console.log(mapped)
console.log(mapped.length)
console.log(Object.keys(mapped).length) 
  
 

所以我期望得到的是mapped.length=4 mapped=[1,2,3,4]和mapped.length=4

但相反,我已经mapped=[1,2,3,4,empty]和mapped.length=5 。

所以我的想法是:在开始时,地图将进行5次迭代,所以它无论如何都会这样做。 这就是我添加console.log(typeof number) 。

但它只执行了4次。

我知道有我预期的结果,过滤器更好。 我只是想知道,这里发生了什么?

I have an algorithm :

let someArray = [1, 2, 3, 4, 5]
let mapped = someArray.map(number => {
    let index = someArray.indexOf(5)
    if (index !== -1) {
        someArray.splice(index, 1)
    }
    console.log(typeof number)
    return number
})
console.log(mapped)
console.log(mapped.length)
console.log(Object.keys(mapped).length) 
  
 

So what I expected to have was mapped=[1,2,3,4] and mapped.length=4

But instead I have mapped=[1,2,3,4,empty] and mapped.length=5.

So what I thought is : in the beginning, map is going for 5 iterations so it does it no matter what. That's why I added console.log(typeof number).

But it's executed only 4 times.

I know to have my expected result, filter is way better. I'm just wondering, what is happening here ?

最满意答案

请参阅MDN文档 :

map按顺序为数组中的每个元素调用一次提供的回调函数,并从结果中构造一个新数组。 仅对已分配值的数组的索引(包括undefined)调用回调。 它不会被调用缺少数组的元素(即,从未设置过的索引,已删除的索引或从未赋值的索引)。

如果更改了数组的现有元素,则传递给回调的值将是映射访问它们时的值。 不会访问在调用map之后和访问之前删除的元素。

当你迭代它时你正在改变数组,这意味着一旦达到index [4],那个元素(其值曾经是5)就不再存在了,这意味着函数不会被调用那次迭代,导致<empty> 。 生成的数组使用5个元素创建,但从不在最后一个元素上调用回调。

请改用filter 。

See MDN documentation:

map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values, including undefined. It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value).

If existing elements of the array are changed, their value as passed to callback will be the value at the time map visits them. Elements that are deleted after the call to map begins and before being visited are not visited.

You're mutating the array as you're iterating over it, which means that once index [4] is reached, that element (whose value used to be 5) doesn't exist anymore, which means the function does not get called on that iteration, resulting in <empty>. The resulting array is created with 5 elements, but the callback is never called on the last element.

Use filter instead.

更多推荐

本文发布于:2023-07-16 12:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   Map   splice   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!