首页 > 解决方案 > 将多个对象作为 1d 传递到数组中

问题描述

我以正确的方式传递数据几乎没有问题。在这里,我的表单以 {comment:'this is my comment'} 的形式出现,id 以数字的形式出现。我需要将它们发布到后端。

    let arr = [];
    let obj = {};

    obj['id'] = this.route.snapshot.params.id;
    arr.push(this.form, obj);

我得到这样的数据:

Array
(
    [0] => Array
        (
            [comment] => this is my comment
        )

    [1] => Array
        (
            [id] => 3
        )

)

我正在寻找一维数组。我可以在后端转换数组,但我正在寻找是否有更好的方法来发布数据。

标签: javascriptphpjquery

解决方案


如果你的目标是让它变成这样

{
    comment: 'this is comment',
    id: 3
}

好吧,您不需要创建数组,只需将它们存储在一个对象中

不要把自己复杂化,就这么简单

var data = {
    comment: this.form.comment
    id: this.route.snapshot.params.id,
};

推荐阅读