javascript - 将多个对象作为 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
)
)
我正在寻找一维数组。我可以在后端转换数组,但我正在寻找是否有更好的方法来发布数据。
解决方案
如果你的目标是让它变成这样
{
comment: 'this is comment',
id: 3
}
好吧,您不需要创建数组,只需将它们存储在一个对象中
不要把自己复杂化,就这么简单
var data = {
comment: this.form.comment
id: this.route.snapshot.params.id,
};
推荐阅读
- r - 当我使用 assignTaxonomy 时 R 会话中止
- if-statement - 得到答案的两个表达式(如果 X = Y 且 A = B,则答案为 D)
- angular - 通过 Ionic 4 中的侧菜单路由页面
- jenkins - 如何在 Jenkins 中制作 QA 自动化测试执行结果图的屏幕截图并将其存档在 Artifacts 中
- asp.net-mvc - 登录asp net mvc后将用户重定向到原始url
- reactjs - 当我们可以使用 useEffect 钩子在其中制作副作用时,React 功能组件如何仍然是“纯函数”?
- javascript - 为什么这个简单的 JSON.parse JavaScript 代码会失败?
- r - 跳过索引列中的 NA 行
- selenium - 如果使用 FindBy 注释,Selenium 检查元素是否存在
- python - 如何在一个长文件中添加多个唯一的 Excel 文件?(Python)