有没有一种方法可以将主对象添加到json对象

时间:2020-07-17 17:29:47

标签: javascript arrays json

例如:

let jsonArr = { "one", "two", "three" }

我可以使用JavaScript进行修改以使其{ "body": [ "one", "two", "three" ], "error": null }

2 个答案:

答案 0 :(得分:2)

好吧,考虑到有错字,不是let jsonArr = { "one", "two", "three" },而是 let jsonArr = [ "one", "two", "three" ],可以-为什么?

let jsonArr = [ "one", "two", "three" ]
jsonArr = { body: jsonArr, error: null }

答案 1 :(得分:1)

您的问题应包括您尝试过的事情。

您只需要构建新对象。

例如

let jsonArr = [ "one", "two", "three" ]
const jsonObj = Object.assign({}, {
    body: jsonArr,
    error: null,
  });