简体   繁体   English

JavaScript推不适用于键值对

[英]Javascript Push not working on Key Value pairs

I am trying to add a key value pair to array called dataxx such as {x:1,y:2},{x:2,y:3} etc. However when I try dataxx.push it doesnt seem to be working - any ideas why or how else I can push to the array? 我正在尝试将一个键值对添加到名为dataxx的数组中,例如{x:1,y:2},{x:2,y:3}等。但是,当我尝试使用dataxx.push时,它似乎不起作用-任何想法,为什么或如何我可以推到阵列?

var dataxx= [{x: 0, y: 0}];

    $( document).ready(function (){
        $.ajax({
            type: "GET",
            url: "https://demo8162910.mockable.io/json",
            dataType: 'jsonp',
            jsonp: true,
            jsonpCallback: "myJsonMethod",
            error: function(){
                alert( 'Unable to load feed, Incorrect path or invalid feed' );
            },
            success: function(data){

                for (i in data)
                {
                    dataxx.push({x: 4, y: 5});
                    //$( "#postlist" ).append(data[i].id);
                }   

                //alert(dataxx);


            }});
    });

i have just add one line of code to see object your code is working fine 我只添加了一行代码以查看对象您的代码工作正常

var dataxx= [{x: 0, y: 0}];

$( document).ready(function (){
    $.ajax({
        type: "GET",
        url: "https://demo8162910.mockable.io/json",
        dataType: 'jsonp',
        jsonp: true,
        jsonpCallback: "myJsonMethod",
        error: function(){
            alert( 'Unable to load feed, Incorrect path or invalid feed' );
        },
        success: function(data){

            for (i in data)
            {
                dataxx.push({x: 4, y: 5});
                //$( "#postlist" ).append(data[i].id);
            }   

            console.log(dataxx);/*object is here */


        }});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM