簡體   English   中英

如何使用 for in 循環動態填充數組

[英]How to dynamically populate an array using a for in loop

為了返回一個包含 1 到 500 之間的隨機數序列的數組set ,我嘗試重構標准 for 循環for(var i = 0; i< 50; i++)並且有效,但是當我嘗試使用for in重構時for in循環中它沒有。 我的猜測是array.length屬性有一些東西,它的用途是我搞砸了。

TL;DR為什么這會返回一個包含 50 個未定義元素的數組,而不是包含 50 個隨機整數的數組? 有沒有辦法讓這種方法奏效?

var set = [];
set.length = 50;

for(var i in set){
    set[i] = Math.floor((Math.random() * 500) + 1);
}

console.log(set);

類似問題:有幫助但不是我想要的

更新

正如我懷疑我遺漏的一點是設置set.length不會向數組添加元素,它只會創建一個稀疏數組(一個有間隙的數組)。 在我的情況下,你不能使用for in因為沒有數組遍歷東西。 我要么必須用虛擬內容(即空字符串)填充數組,或者,更合乎邏輯地,將我試圖用.length屬性實現的范圍部分分離到一個單獨的range變量中。

工作版本

var set = [], range = 50;

for(var i = 0; i < range; i++){
    set[i]=Math.floor((Math.random() * 500) + 1);
}

console.log(set);

在我看來,您想創建一個包含動態內容的靜態大小的數組。 如果是這種情況,您需要先創建具有適當大小的數組。 這將為您創建數組。 但是,您可以使用您想要的隨機值自動填充數組,如下所示:

var N = 50;
var set = Array.apply(null, {length: N}).map(Function.call, Math.random);

數組自動填充后,其余代碼應按預期工作,只需使用數組中隨機數的值即可:

for(var i in set){
    set[i] = Math.floor(set[i] * 500) + 1;
}
console.log(set);
// OUTPUT
// [267, 219, 293, 298, 403, 70, 162, 270, 434, 292, 433, 478, 476, 
//  311, 268, 266, 105, 242, 255, 250, 206, 104, 142, 406, 50, 139, 
//  364, 375, 47, 480, 445, 149, 91, 228, 404, 267, 298, 158, 305, 
//  311, 92, 377, 490, 65, 149, 431, 28, 452, 353, 494]

這是我得到的地方: 創建一個包含 1...N 的 JavaScript 數組

它返回 undefined 因為您正在使用set.length設置數組長度。 調用 this 時,數組的所有元素都undefined

我會完全刪除set.length行。

更新代碼

var set = [];
for (i = 0; i <= 50; i++) {
  set.push(Math.floor(Math.random() * 500))
}

console.log(set)
=> [138, 215, 149, 180, 348, 497, 88, 156, 238, 439, 130, 185, 20, 116, 330, 131, 188, 257,
    260, 1, 469, 482, 208, 494, 26, 374, 281, 403, 403, 137, 156, 243, 378, 281, 329,
    84, 471, 429, 120, 381, 456, 471, 36, 395, 299, 497, 151, 210, 80, 310]

for in這里似乎不是您想要的。 嘗試for循環:

var set = [], 
    i = 0, 
    l = null;

set.length = 50;

for(; l = set.length; i < l; i++){
    set[i] = Math.floor((Math.random() * 500) + 1);
}

console.log(set);

問題不在於array.length屬性,而是這種擴大數組的方法不會創建“屬性”。 請注意以下事項:

> [ /*hole*/ , /*hole*/ ,] // An array with two "holes"
[ ,  ]
> new Array(2) // Another way to create an array with two holes
[ ,  ]
> [ undefined, undefined ] // An array with two undefined elements -- not the same
[ undefined, undefined ]
> Object.getOwnPropertyNames([undefined, undefined]);
[ '0', '1', 'length' ]
> Object.getOwnPropertyNames([,,]);
[ 'length' ] // Doesn't have any integer properties!

您正在做的是創建一個具有零元素的數組,然后將其擴展為包含 50 個“孔”,就像具有兩個孔的數組一樣。 由於此數組沒有可枚舉的屬性( length不可枚舉),因此您無法使用for...in對其進行迭代。 最終,您最好使用傳統的for循環。

編輯

我可以當場想到的“洞”的最佳定義是數組的不存在屬性,其名稱是整數i使得0 <= i && i < array.length (考慮到這一點,重要的是要注意 JavaScript 數組只是具有稱為“索引”的數字屬性和一些額外的特殊功能的對象。)

屬性引用返回 undefined 有兩種方式:屬性不存在或屬性存在並包含值 undefined。 因此:

> myObject = { myProp: 1, otherProp: undefined }
{ myProp: 1, otherProp: undefined }
> myObject.nonExistentProperty
undefined
> myObject.otherProp
undefined

現在,一個洞是一個不存在的屬性,就像上面的myObject.nonExistentProperty一樣,所以嘗試訪問它會導致它返回undefined

第二個鏈接似乎有你的答案。

for (var item in array)查看您的數組,對於數組中的每個項目,將該項目存儲在“item”中,並讓您遍歷每個項目。 設置 array.length 不會為您提供數組中的任何項目,因此for in根本不執行 - 沒有任何可迭代的內容。

用填充值填充數組的最佳和最簡單的方法可以簡單地通過實例化Array()類然后使用Array.protype.fill()方法為其賦值。

const array_filler = new Array(10).fill(20)

如需進一步參考,您可以閱讀以下文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM