简体   繁体   中英

Get key of javascript object forEach

I am using Taffy DB, and have a search feature which searches for a property:

var finded = properties({type:"small"}).get();

finded.forEach( function() {
    var name = this['name'];
    alert('The matched result is ' + name + '.');
});

The the first returns two javascript objects, for both properties found.

The next section of code (line 3-) is supposed to perform a function on each object retrieved to get its 'Name' key and then post it in an alert box.

However, instead it just comes out blank. There isn't [object Object] , there isn't undefined it's just blank (with the exception of the quoted text, of course.)

What am I doing wrong?

this is not refer to an array or to the element of that array, instead if you don't provide second argument of Array.prototype.forEach , callback function will be executed in global context.

finded.forEach(function (value) {
    var name = value.name;
});

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