开发者

Javascript push method

开发者 https://www.devze.com 2023-03-08 20:35 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消