簡體   English   中英

item有值但在Uint8Array中返回'undefined'

[英]item has value but in Uint8Array return 'undefined'

嗨我有這樣的Uint8Array

var ar = new Uint8Array();
ar[0] = 'G';
ar[1] = 0x123;

第二個索引是一個十六進制數,我想檢查ar[1]是否大於零或不是,所以我寫這個代碼:

if(ar[1] > 0){
  console.log("OK");
}
else{
  console.log("NOP")
}

但如果我寫console.log(ar[1])我會'未定義'。 這是我創建的一個簡單的jsbin

AFAIK需要將條目數作為參數傳遞給構造函數。

 const ar = new Uint8Array(2); ar[0] = 'G'; ar[1] = 0x123; console.log(ar[1]); if(ar[1] > 0){ console.log("OK"); } else{ console.log("NOP") } 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

編輯:再次閱讀文檔,你可以使用空構造函數new Uint8Array(); // new in ES2017 new Uint8Array(); // new in ES2017 但是沒有任何有效的例子。

您可以使用UintArray.from()或將元素數傳遞給構造函數,然后使用括號表示法設置索引的值

 var ar = Uint8Array.from(["G", 0x123]); if (ar[1] > 0) { console.log("OK"); } else { console.log("NOP") } 

暫無
暫無

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

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