Javascript推送方法

问题描述

| 我在这里做错了什么?我的数组是空的。
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);
    

解决方法

javascript区分大小写,因此InfoArray和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}]

 // 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);
    ,我不认为
InfoArray = var hotelinfo = [{\"address\":\"07288 Albertha Station\",{MORE INFO}]
是有效的JavaScript。您必须将其拆分:
var hotelinfo;
InfoArray = hotelinfo = [{\"address\":\"07288 Albertha Station\",{MORE INFO}]
    ,JavaScript区分大小写,请使用
$.each(InfoArray,value){
即InforArray与inforarray不同。还行
InfoArray = var hotelinfo = 
应该
InfoArray = hotelinfo = 
    ,在表达式中间声明变量是无效的。 var关键字必须在一行的开头。在一行中声明多个变量是完全有效的,但是var关键字必须是该行的第一个。