简体   繁体   中英

Array.each outputs all methods

Why would...

for(var k in this.errors) {
        $('error_list').insert({
            bottom: new Element('li').update(k + ' :'+this.errors[k])
        })
    }

...output put all Prototype enumerable methods, plus what I've added to the array?

I'm building an associative array:

this.errors['email'] = 'Your email is invalid';

您需要使用“ hasOwnProperty”来防止这种情况。

You may prevent this using hasOwnProperty :

for(var k in this.errors) {
    if (this.errors.hasOwnProperty(k)) {
        $('error_list').insert({
            bottom: new Element('li').update(k + ' :'+this.errors[k])
        })
    }
}

Try this:

$H(this.errors).each(function(error) {
    $('error_list').insert({
        bottom: new Element('li').update(error.key + ': ' + error.value)
    })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM