javascript - Array not responding to forEach -


here's test in chrome dev console:

> tags returns ["test_tag", "test_tag2"]  > tags.foreach returns undefined  > ["test_tag", "test_tag2"].foreach returns foreach() { [native code] } 

i have no idea why tags object not responding foreach.

checking type not instructive, expected:

> typeof(tags) // returns 'object' > typeof(["test_tag", "test_tag2"]) // returns 'object' 

how constructing tags object?

var $nodes = $(".metadata") var tags = $nodes.map(function(idx, node){   nodejson = $(node).text()   return json.parse(nodejson)['tags'] }) 

jquery's map() function returns collection of elements, or in case returned text wrapped in jquery object, of course object, not array.

if wanted text in array, you'd use get() well

var $nodes = $(".metadata") var tags = $nodes.map(function(idx, node){     var nodejson = $(node).text();     return json.parse(nodejson)['tags']; }).get(); 

or more appropriate $.map

var $nodes = $(".metadata") var tags = $.map( $nodes, function(node){     var nodejson = $(node).text();     return json.parse(nodejson)['tags']; }); 

Comments