项目具有价值,但在Uint8Array回报“未定义”

问题描述:

您好我有Uint8Array这样项目具有价值,但在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; }

编辑:再次读取docs可以使用空的构造new Uint8Array(); // new in ES2017。但是没有任何工作的例子。

可以使用UintArray.from()或通过元件的数量来构造,然后用括号表示法

var ar = Uint8Array.from(["G", 0x123]); 
 

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

设置索引的值