1

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

4 Answers 4

2

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}]
Sign up to request clarification or add additional context in comments.

1 Comment

This was an error in my transcription between my console and stackoverflow. The infoarray is already the same case as in the funciton. This is not working.
1

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

Comments

0

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 = 

1 Comment

another good point... 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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.