English 中文(简体)
Java :将阵列的界定价值分开,这些价值不明确,而且阵列价值没有界定
原标题:Javascript : separating array defined value that s undefined and array value that s not defined

How do I now check if the value is defined as undefined, or if it s really not defined?
eg.

var a = [];
a[0] = undefined;

// a defined value that s undefined
typeof a[0] === "undefined";

// and a value that hasn t been defined at all
typeof a[1] === "undefined";

是否有办法将这两者分开? 可能利用旁边的通道穿透阵列,但情况如何?

最佳回答

您可使用<代码>in的操作员,检查在阵列是否存在某一指数,而不论该指数的实际价值如何。

var t = [];
t[0] = undefined;
t[5] = "bar";

console.log( 0 in t ); // true
console.log( 5 in t ); // true
console.log( 1 in t ); // false
console.log( 6 in t ); // false

if( 0 in t && t[0] === undefined ) {
     // the value is defined as "undefined"
}

if( !(1 in t) ) {
    // the value is not defined at all
}
问题回答

如果指数是特定阵列的话,可以检查:

0 in a // => true
1 in a // => false




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签