JZ.
JZ.

Reputation: 21907

Javascript push method

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);

Upvotes: 1

Views: 1256

Answers (4)

Metagrapher
Metagrapher

Reputation: 8932

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.

Upvotes: 0

Raze
Raze

Reputation: 2224

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}]

Upvotes: 2

amit_g
amit_g

Reputation: 31270

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 = 

Upvotes: 0

Metagrapher
Metagrapher

Reputation: 8932

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);

Upvotes: 1

Related Questions