Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的数组到对象的数组_Javascript_Jquery - Fatal编程技术网

javascript中的数组到对象的数组

javascript中的数组到对象的数组,javascript,jquery,Javascript,Jquery,我试图创建一个数组,其中包含多个数组,这些数组被添加到每个循环中,然后将信息传递给ajax发布 但是,它不会添加到请求中,当我在数组上尝试JSON.Stringify时,它只返回空 我做错了什么 谢谢 var graphics = new Array(); var customer = "Joe Bloggs"; var email = "joe@bloggs.com"; var contactnumber = "12345"; $('.graphi

我试图创建一个数组,其中包含多个数组,这些数组被添加到每个循环中,然后将信息传递给ajax发布

但是,它不会添加到请求中,当我在数组上尝试JSON.Stringify时,它只返回空

我做错了什么

谢谢

var graphics = new Array();
var customer        = "Joe Bloggs";
var email           = "joe@bloggs.com";
var contactnumber   = "12345";    
$('.graphic').each(function (i) {
    var graphic = new Array();    
    graphic['name']         = $(this).attr('data-name');
    graphic['type']         = $(this).attr('data-type');
    graphic['price']        = $(this).attr('data-price');
    graphic['model']        = $(this).attr('data-model');
    graphic['carcolor']     = $(this).attr('data-carcolor');
    graphic['graphiccolor'] = $(this).attr('data-color');
    graphic['reg']          = $(this).attr('data-reg');
    graphic['note']         = $(this).attr('data-note');    
    graphics.push(graphic);
});    
$.ajax({
    type: "POST",
    data: {
        customer:customer, 
        email:email, 
        contactnumber:contactnumber, 
        graphics:graphics
    },
    url: "assets/php/index.php",
    success: function(msg){
        $('.answer').html(msg);
    }
});
中的图形。每个都是一个数组,但您将其视为一个对象。只需将新数组更改为{},它将更加正确,但仍然不是一种可接受的格式。要正确发布,您需要在发布之后将其序列化为JSON

    $('.graphic').each(function (i) {
            var graphic = {};

            graphic['name']         = $(this).attr('data-name');
            graphic['type']         = $(this).attr('data-type');
            graphic['price']        = $(this).attr('data-price');
            graphic['model']        = $(this).attr('data-model');
            graphic['carcolor']     = $(this).attr('data-carcolor');
            graphic['graphiccolor'] = $(this).attr('data-color');
            graphic['reg']          = $(this).attr('data-reg');
            graphic['note']         = $(this).attr('data-note');

            graphics.push(graphic);
    });

    $.ajax({
        type: "POST",
        data: {customer:customer, email:email, contactnumber:contactnumber, graphics:JSON.stringify(graphics)},
        url: "assets/php/index.php",
        success: function(msg){
            $('.answer').html(msg);
        }
    });
中的图形。每个都是一个数组,但您将其视为一个对象。只需将新数组更改为{},它将更加正确,但仍然不是一种可接受的格式。要正确发布,您需要在发布之后将其序列化为JSON

    $('.graphic').each(function (i) {
            var graphic = {};

            graphic['name']         = $(this).attr('data-name');
            graphic['type']         = $(this).attr('data-type');
            graphic['price']        = $(this).attr('data-price');
            graphic['model']        = $(this).attr('data-model');
            graphic['carcolor']     = $(this).attr('data-carcolor');
            graphic['graphiccolor'] = $(this).attr('data-color');
            graphic['reg']          = $(this).attr('data-reg');
            graphic['note']         = $(this).attr('data-note');

            graphics.push(graphic);
    });

    $.ajax({
        type: "POST",
        data: {customer:customer, email:email, contactnumber:contactnumber, graphics:JSON.stringify(graphics)},
        url: "assets/php/index.php",
        success: function(msg){
            $('.answer').html(msg);
        }
    });

看起来你已经习惯了PHP

定义数组时:

var graphic = new Array();
您需要将其视为一个数组:

graphic[0] = "foo";
graphic[1] = "bar";
…而不是像一个物体:

var graphic = {};
graphic['my_property'] = "foobar"
执行此操作时:

var a = new Array();
a["something"] = 123;
那么a将是一个空数组[],但a.something将包含123

但是,当您想要字符串化a,并且a是一个数组时,它将尝试查找该数组的字符串化表示形式。因为它是空的,所以得到[]


所以,为了解决您的问题,只需更改图形,使其成为一个对象。请注意,您最终得到的是一个对象数组,而不是数组数组。

看起来您已经习惯了PHP

定义数组时:

var graphic = new Array();
您需要将其视为一个数组:

graphic[0] = "foo";
graphic[1] = "bar";
…而不是像一个物体:

var graphic = {};
graphic['my_property'] = "foobar"
执行此操作时:

var a = new Array();
a["something"] = 123;
那么a将是一个空数组[],但a.something将包含123

但是,当您想要字符串化a,并且a是一个数组时,它将尝试查找该数组的字符串化表示形式。因为它是空的,所以得到[]


所以,为了解决您的问题,只需更改图形,使其成为一个对象。请注意,您最终得到的是一个对象数组,而不是数组数组。

您混淆了数组和对象。var graphic=新数组应该是var graphic={}-否则它将无法正确序列化。每个数组中的graphic是一个数组,但是您将ti视为一个对象。只需将新数组更改为{},它将更加正确,但仍然不是一种可接受的格式。要正确发布,您需要将其序列化为JSON。这会混淆数组和对象。var graphic=新数组应该是var graphic={}-否则它将无法正确序列化。每个数组中的graphic是一个数组,但是您将ti视为一个对象。只需将新数组更改为{},它将更加正确,但仍然不是一种可接受的格式。要正确发布,您需要将其序列化为JSON。完成了!非常感谢:调用新数组和{}有什么不同?首先,新数组创建一个数组,{}创建一个对象。字符串化时,存储在数组上的属性将被忽略,这就是为什么字符串化时数组看起来是空的。@GraemeLeighfield:{}类似于新对象,就像[]类似于新数组一样!非常感谢:调用新数组和{}有什么不同?首先,新数组创建一个数组,{}创建一个对象。字符串化时,数组中存储的属性将被忽略,这就是为什么字符串化时数组看起来是空的。@GraemeLeighfield:{}类似于新对象,就像[]类似于新数组