简体   繁体   English

在 javascript 中更改特定数组的结构

[英]Change structure of particular array in javascript

Unfortunately, due to a plugin that I'm using, I have to serialize form names and values in a particular format for them to be saved in a database.不幸的是,由于我正在使用的插件,我必须以特定格式序列化表单名称和值,以便将它们保存在数据库中。 Say I have the form:说我有表格:

<form>
    <input type="text" name="ratings[check-in]">
    <input type="text" name="ratings[location]">
    <textarea type="text" name="comment" placeholder=""></textarea>
    <input type="text" name="listing_id" value="9297">
</form>

currently I'm serializing this as follows:目前我正在将其序列化如下:

var valueform = $('form#addmyreview').serializeArray();

which produces the following structure:产生以下结构:

stdClass Object
(
    [0] => Array
        (
            [name] => ratings[check-in]
            [value] => 
        )

    [1] => Array
        (
            [name] => ratings[location]
            [value] => 
        )

    [2] => Array
        (
            [name] => listing_id
            [value] => 9483
        )

    [3] => Array
        (
            [name] => comment
            [value] => 
        )

)

However according to the plugin the form data has to be serialised into the following array example:但是根据插件,表单数据必须序列化为以下数组示例:

stdClass Object
(
    [ratings] => Array
        (
            [check-in] => 
            [location] => 
        )

    [listing_id] => 9297
    [comment] => wedf
)

As you can see this is far more succinct and the data can then be used by the plugin.如您所见,这更加简洁,然后插件可以使用数据。 Can anyone help with a way to serialize this object?任何人都可以帮助序列化这个 object 吗? I have tried the following:我尝试了以下方法:

function serializeObject(obj) {
    var jsn = {};
    $.each(obj, function() {
        if (jsn[this.name]) {
            if (!jsn[this.name].push) {
                jsn[this.name] = [jsn[this.name]];
            }
            jsn[this.name].push(this.value || '');
        } else {
            jsn[this.name] = this.value || '';
        }
    });
    return jsn;
};

but this produced:但这产生了:

stdClass Object
(
    [ratings[sanitation] => 
    [ratings[food] => 
    [ratings[reservation-available] => 
    [ratings[wait-time] => 
    [ratings[value] => 
    [ratings[staff-service] => 
    [listing_id] => 9483
    [comment] => 
)

so it's a big improvement but not quite there:-(所以这是一个很大的改进,但还不够:-(

Use serialize instead serializeArray to post the form.使用serialize而不是serializeArray来发布表单。 The output of serialize is the same as you need in the backend. serialize的output跟你后端需要的一样。

From your output example, I assume your backend language is PHP , serialize and serializeArray are jQuery methods, where serialize is principally used for sending information by ajax , see code example below.从您的 output 示例中,我假设您的后端语言是PHP , serialize 和 serializeArray 是jQuery方法,其中 serialize 主要用于通过ajax发送信息,请参见下面的代码示例。

Form HTML:表格 HTML:

<form id="addmyreview">
    <input type="text" name="ratings[check-in]">
    <input type="text" name="ratings[location]">
    <textarea type="text" name="comment" placeholder=""></textarea>
    <input type="text" name="listing_id" value="9297">
</form>

Javascript jQuery: Javascript jQuery:

$.ajax({
    type: 'post',
    data: $('#addmyreview').serialize()
}).done(function(result) {
    // do something with result
});

In the backend PHP:在后端 PHP:

$data = (object) $_POST;

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

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