Javascript howto:js中的push()函数不插入值

Javascript howto:js中的push()函数不插入值,javascript,jquery,google-maps,push,Javascript,Jquery,Google Maps,Push,我正在尝试使用Google Maps API,我被困在这里: 我需要一个数组变量(waypts),具有特定格式的特定值 当我将一个值“推”到我的waypts变量中时,它只返回“OBJECT”作为其值 我需要它来推送所选输入文本中设置的实际值 使用jQuery和javascript(核心) 代码: <html> <head> <script type="text/javascript" src="jquery.js"></script>

我正在尝试使用Google Maps API,我被困在这里:

  • 我需要一个数组变量(
    waypts
    ),具有特定格式的特定值
  • 当我将一个值“推”到我的
    waypts
    变量中时,它只返回“OBJECT”作为其值
  • 我需要它来推送所选输入文本中设置的实际值
使用jQuery和javascript(核心)

代码:

<html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $('document').ready(function(){
            var waypts = [];
            var temp = $('input.boom').map(function(){
                return $(this).val();
            });

            for (var i=0;i<temp.length;i++){
                waypts.push({
                    location:temp[i].value,
                    stopover:true
                });
            }
            alert(waypts);
        });
    </script>
</head>
<body>
    <input type="text" class="boom" value="boom1"><br>
    <input type="text" class="boom" value="boom2"><br>
    <input type="text" class="boom" value="boom3"><br>
    <input type="text" class="boom" value="boom4"><br>
</body>
</html>

$('document').ready(函数(){
var-waypts=[];
var temp=$('input.boom').map(函数(){
返回$(this.val();
});

对于(var i=0;i您需要提醒数组的属性

尝试向waypts[0]发出警报。位置

要显示对象数组的所有项,请执行以下操作:

var output="";
for (var o in waypts) {
  if (waypts.hasOwnProperty(o) {
    output += "\n"+o+":"+waypts[o].location + '-' + waypts[o].stopover;
  }
}
alert(output)
或者对于标准数组(就像我读到你的问题时那样)


waypts
数组中的每个元素都将是一个对象-您需要单独引用对象中的每个值。尝试用此替换
警报(waypts)
,以查看数组中的所有数据:

for (var i = 0; i <= waypts.length; i++) {
    alert(waypts[i].location);
    alert(waypts[i].stopover);
}

for(var i=0;i在当前代码中,
waypts
将是一个对象数组,每个对象都具有
location
属性和
stop
属性

您可以将代码更改为以下内容以使其更干净

var waypts = $('input.boom').map(function(){
     return {
        location: $(this).val(),
        stopover: true
     };
}).get();

// waypts is now an array of objects with `location` and `stopover` attributes.
// you can see what each value is by alerting `waypts[i].location` and `waypts[i].stopover`.
当你说

waypts.push({
                location:temp[i].value,
                stopover:true
            });
您正在将一个新的文本对象(包含在
{}
中)推送到
waypts

因此,您正在将新项目添加到这些位置/中途停留对象的数组中


要实际引用
位置
中途停留
值,您需要首先使用
[]
索引到
路径点
(例如
路径点[0]
将指向第一个这样的对象)。其次,您需要引用所需的属性,可以是
location
stop
。您可以使用标准的“点表示法”执行此操作。例如
waypts[0]。location
将为您提供
waypts
数组中第一项的位置值。

在代码中,您需要使用
location:temp[i]
而不是
位置:temp[i].value
。然后你会得到合适的值。是啊,我注意到了。再次感谢。非常感谢。我将你的答案与rory的(在你的之后)合并,得到了我想要的答案。不客气。我用你的数组增强了答案
var waypts = $('input.boom').map(function(){
     return {
        location: $(this).val(),
        stopover: true
     };
}).get();

// waypts is now an array of objects with `location` and `stopover` attributes.
// you can see what each value is by alerting `waypts[i].location` and `waypts[i].stopover`.
waypts.push({
                location:temp[i].value,
                stopover:true
            });