数组中什么是空的?例如[空×5]

问题描述 投票:0回答:2

很抱歉这里的标题具有误导性,我无法构建任何合适的标题。

当数组中没有任何内容(由

empty × n
打印)但它们有长度时,我对数组感到困惑。
例如我通过
const a = [,,,]
创建数组。这将创建一个长度为 3 但内部没有任何内容的数组。如果我在浏览器控制台中打印它,它会打印以下内容:

enter image description here

What does empty mean here?
如果我运行 map 或 forEach 函数并尝试控制台某些内容,我什么也得不到。

添加了一些代码。

const a = [,,,]

console.log("print a: ",a)
console.log("print a.length: ",a.length)
console.log("print typeof a[0]: ", typeof a[0])
console.log("a.forEach((data, index) => { console.log(data, index) }): ", a.forEach((data, index) => { console.log(data, index) }))
console.log("")


const b = [undefined, undefined, undefined]

console.log("print b: ", b)
console.log("print b.length: ", b.length)
console.log("print typeof b[0]: ", typeof b[0])
console.log("b.forEach((data, index) => { console.log(data, index) }): ", b.forEach((data, index) => { console.log(data, index) }))
console.log("")

console.log("compare a[0] and b[0]: ", a[0] === b[0])

唯一不同的是当我打印 a 和 b 时(尽管 stackoverflow 控制台打印它们相同,但浏览器控制台打印不同)以及当我尝试循环遍历数组时。 momentjs isEqual 也使它们相等(jsfiddle here

我的主要疑问是:

  • 它是什么类型的数组?
  • 这里的空是什么意思?
  • 它与具有所有未定义值或空数组的数组有何不同?或者不是吗?
  • 我们是否使用它或任何示例用例

我已经阅读了有关 null 和未定义数组值的内容并理解了它。但对于这个,我还没有找到合适的东西。我发现的大部分搜索都与 const a = [] 是一个空数组或如何检查数组是否为空等相关。

因此,如果有人可以解释或提供任何适当的阅读链接,这将非常有帮助。

如果我需要添加其他内容,请告诉我。

javascript arrays properties sparse-matrix
2个回答
9
投票

稀疏数组简介

首先澄清一下您创建的内容称为“稀疏数组”。简而言之,稀疏数组与普通数组类似,但并非所有索引都有数据。在某些情况下,比如 JavaScript,这会导致对它们的处理稍微更重要。其他语言只是有一个固定长度的普通数组,其中一些值在某种意义上是“零”(取决于什么值对于特定数组来说可以表示“无” - 可能是 0

null
""
,等)。
空槽位

稀疏数组中的空槽顾名思义就是没有填充数据的槽。与大多数其他实现不同,JavaScript 数组的大小不固定,甚至可能有一些索引“缺失”。例如:

const arr = []; // empty array arr[0] = "hello"; // index 0 filled arr[2] = "world"; // index 2 filled 你会得到一个没有索引1的数组。它不是

null
,也不是空的,它不存在。这与当你有一个对象
没有

属性时得到的行为相同:

const person = {foo: "hello"};

您有一个具有属性 

foo
 的对象,但它没有,例如,
bar

属性。和之前的数组没有索引完全一样

1
JavaScript 表示“未找到值”的唯一方式是使用 
undefined
,但是这会混淆

“该属性存在并且分配给它的值为 
undefined

  • “该属性根本不存在”
    
    
  • 举个例子:

const person1 = { name: "Alice", age: undefined }; const person2 = { name: "Bob" }; console.log("person1.age", person1.age); console.log("person2.age", person2.age); console.log("person1.hasOwnProperty('age')", person1.hasOwnProperty('age')); console.log("person2.hasOwnProperty('age')", person2.hasOwnProperty('age'));


在任何一种情况下尝试解决
undefined

时,您都会得到

age

,但原因不同。

由于 JavaScript 中的数组
对象,因此你会得到相同的行为:

const arr = []; // empty array arr[0] = "hello"; // index 0 filled arr[2] = "world"; // index 2 filled console.log("arr[1]", arr[1]); console.log("arr.hasOwnProperty(1)", arr.hasOwnProperty(1));


为什么重要

稀疏数组在 JavaScript 中得到不同的处理。也就是说,迭代项目集合的数组方法将仅遍历

filled

槽,并忽略空槽。这是一个例子:

const sparseArray = []; // empty array sparseArray[0] = "hello"; // index 0 filled sparseArray[2] = "world"; // index 2 filled const arr1 = sparseArray.map(word => word.toUpperCase()); console.log(arr1); //["HELLO", empty, "WORLD"] const denseArray = []; // empty array denseArray[0] = "hello"; // index 0 filled denseArray[1] = undefined; // index 1 filled denseArray[2] = "world"; // index 2 filled const arr2 = denseArray.map(word => word.toUpperCase()); //error console.log(arr2);


如您所见,迭代稀疏数组很好,但如果数组中有显式
undefined

,则

word => word.toUpperCase()

将失败,因为

word
undefined
如果您有要运行的数字索引数据 
.filter

.find

.map
.forEach
等,稀疏数组非常有用。我们再举例说明一下:

//some collection of records indexed by ID const people = []; people[17] = { id: 17, name: "Alice", job: "accountant" , hasPet: true }; people[67] = { id: 67, name: "Bob" , job: "bank teller", hasPet: false }; people[3] = { id: 3 , name: "Carol", job: "clerk" , hasPet: false }; people[31] = { id: 31, name: "Dave" , job: "developer" , hasPet: true }; /* some code that fetches records */ const userChoice = 31; console.log(people[userChoice]); /* some code that transforms records */ people .map(person => `Hi, I am ${person.name} and I am a ${person.job}.`) .forEach(introduction => console.log(introduction)); /* different code that works with records */ const petOwners = people .filter(person => person.hasPet) .map(person => person.name); console.log("Current pet owners:", petOwners)


    

它就是这样

empty

0
投票
undefined

也不是

null
const a = [,,,,]
const a = new Array(4)
 相同
这里 
a
 是一个没有填充任何元素且仅具有 
length
属性
 的数组
这样做,
let arr1 = new array()
,然后
console.log(arr1.length)
,你将得到 0 作为输出。 如果你这样做
console.log(arr1)
你会得到
[ <4 empty items> ]
如果您像这样更改 arr1 的长度属性
arr1.length = 4
您将得到一个空数组,其长度属性 = 4,但没有填充任何项目,因此这些插槽将是
empty
如果您这样做
console.log(typeof(arr1[0])
您会得到
undefined 
只是因为没有其他可能的类型可以显示。并且 Array 的任何方法都不会应用于包含空元素的数组

所以,

空数组意味着具有 length 属性且未填充槽的数组

这与带有

undefined

的数组不同,因为在 JS 中

undefined

是一种类型,您可以通过调用其上的所有数组方法来执行并获得结果,而带有

empty
元素的数组没有类型,并且没有数组方法可以执行应用于其上。
    

© www.soinside.com 2019 - 2024. All rights reserved.