What am I doing wrong here? my array is empty.
var infoarray = [{"address":"07288 Albertha Station","city":"Littelside","created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"},{MORE INFO}]
// Populates myarray from infoarray ruby object
var myarray = 开发者_开发问答new Array();
$(document).ready(function(){
$.each(infoarray,function(key,value){
myarray.push(value['city'])
});
});
console.log(myarray);
I don't think
InfoArray = var hotelinfo = [{"address":"07288 Albertha Station","city":"Littelside","created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"},{MORE INFO}]
is valid JavaScript. You'll have to split it:
var hotelinfo;
InfoArray = hotelinfo = [{"address":"07288 Albertha Station","city":"Littelside","created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"},{MORE INFO}]
javascript is case sensitive, so InfoArray and infoarray are different variables.
Does this work?:
var hotelinfo = [{"address":"07288 Albertha Station","city":"Littelside","created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"},{MORE INFO}]
// Populates myarray from infoarray ruby object
var myarray = new Array();
$(document).ready(function(){
$.each(hotelinfo,function(key,value){
myarray.push(value['city'])
});
});
console.log(myarray);
Javascript is case sensitive so use
$.each(InfoArray,function(key,value){
i.e. InforArray is not same as inforarray. Also the line
InfoArray = var hotelinfo =
should be
InfoArray = hotelinfo =
it's invalid to declare a variable in the middle of an expression. The var keyword must be at the beginning of a line. It is perfectly valid to declare multiple variables in a single line, but the var keyword must be the first on the line.
精彩评论