为什么`.forEach`在密集数组上工作但在稀疏数组上不工作? [英] Why does `.forEach` work on dense arrays but not on sparse arrays?

查看:47
本文介绍了为什么`.forEach`在密集数组上工作但在稀疏数组上不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解'空'稀疏数组(例如 new Array(3))和等效的'空'密集数组(数组之间的区别) 3个未定义的条目)。



我可以用这两种方式创建一个包含3个未定义值的数组:

  var sparse = new Array(3); 
//或
var sparse = [,,,];

var dense = Array.apply(null,Array(3)); //请参阅
下面的密集数组链接

密集数组



如果我为其中任何一个执行console.log结果是:

  [undefined,undefined,undefined] 

如果我遍历每个数组以将其与另一个数组进行比较,它们将严格匹配:

  console.log(sparse.length === dense.length); 

// true

for(var i = 0; i< dense.length; i ++){
console.log(i +':'+ (密集[i] ===稀疏[i]));
}

//'0:true'
//'1:true'
//'2:true'

但是,如果我使用 .forEach (或地图 reduce 等)然后回调将永远不会在稀疏数组上调用,但会在密集数组上调用三次:

  sparse.forEach(function(val,i){
console.log(i +':'+ val);
});

//没什么。无操作。

dense.forEach(function(val,i){
console.log(i +':'+ val);
});

//'0:undefined'
//'1:undefined'
//'2:undefined'

所以我的问题是:


  1. 如果它们都有相同的长度,索引和值是如何迭代的,但另一个不是?

  2. 差异的原因是什么?

  3. 什么是最好的确定数组是稀疏还是密集的方法?


解决方案



  1. 如果它们都有相同的长度,索引和值,那么一个是可迭代的,但另一个不是?




这些函数是明确记录以这种方式执行。两个数组都是可迭代的,但 forEach / map / etc显式跳过不在数组中的索引:

  var sparse = new Array(3); 
var dense = Array.apply(null,Array(3)); //参见密集数组链接belo

1稀疏; // false
1密集; // true

//稀疏仍然可以迭代
稀疏[100] ='a'
sparse.forEach(function(n,i){console.log(你) }); // 100





  1. 差异的原因是什么?


据推测,明确决定省略指数哪个不存在



  1. 确定阵列是否最佳的最佳方法是什么?稀疏还是密集?


您可以使用以下内容:

  function isSparse(array){
for(var i = 0; i< array.length; ++ i){
if(
if( !我在数组中)
返回true;
返回false;
}






RE:您的评论


为什么[,,,]没有索引但[undefined,undefined,undefined]确实存在原因。


必须是一个理由,这就是它们的构建方式。一个有钥匙,另一个有钥匙。



看:

  Object.keys(Array(3))// => [] 
Object.keys(['a','b'])// => [0,1]


I'm trying to understand the difference between an 'empty' sparse array (e.g. new Array(3)) and an equivalent 'empty' dense array (array with 3 undefined entries).

I can create an array with 3 undefined values these two ways:

var sparse = new Array(3);
// or
var sparse = [,,,];

var dense = Array.apply(null, Array(3)); // See dense array link below

Dense Arrays

If I do console.log for either of these the result is:

[undefined, undefined, undefined]

If I loop over each array to compare it against the other one they will strictly match:

console.log(sparse.length === dense.length);

// true

for (var i = 0; i < dense.length; i++) {
  console.log(i +':'+ (dense[i] === sparse[i]));
}

// '0:true'
// '1:true'
// '2:true'

However, if I use .forEach (or map, reduce, etc) then the callback will never be called on the sparse array but will be called three times on the dense one:

sparse.forEach(function(val,i){
   console.log(i +':'+ val);
});

// Nothing. No-op.

dense.forEach(function(val,i){
   console.log(i +':'+ val);
});

// '0:undefined'
// '1:undefined'
// '2:undefined'

So my questions are:

  1. If they both have the same length, indexes, and values how is one iterable but the other isn't?
  2. What is the reason for the difference?
  3. What is the best way to determine if an array is sparse or dense?

解决方案

  1. If they both have the same length, indexes, and values how is one iterable but the other isn't?

These functions are explicitly documented to perform this way. Both arrays are iteratable, but forEach/map/etc explicitly skips indices which are not in the array:

var sparse = new Array(3);
var dense = Array.apply(null, Array(3)); // See dense array link belo

1 in sparse; // false
1 in dense; // true

// Sparse can still be iterated over
sparse[100] = 'a'
sparse.forEach(function (n, i) { console.log(n, i) }); // a 100

  1. What is the reason for the difference?

Presumably an explicit decision was made to omit indices which don't exist

  1. What is the best way to determine if an array is sparse or dense?

You can use the following:

function isSparse(array) {
  for (var i = 0; i < array.length; ++i) {
    if (!i in array)
      return true;
  return false;
}


RE: Your comment

There has got be a reason for why [,,,] doesn't have indexes but [undefined, undefined, undefined] does.

There doesn't have to be a reason, that's just the way they're built. One has keys, the other doesn't.

Look:

Object.keys(Array(3)) // => []
Object.keys(['a','b']) // => ["0", "1"]

这篇关于为什么`.forEach`在密集数组上工作但在稀疏数组上不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆