我现有的来自响应的json数组是:
comments:[{id: "3124fac5-9d3e-4fa9-8a80-10f626fbf141", createdDate: 1469606019000,…},…]
0:{id: "3124fac5-9d3e-4fa9-8a80-10f626fbf141", createdDate: 1469606019000,…}
createdBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
createdDate:1469606019000
id:"3124fac5-9d3e-4fa9-8a80-10f626fbf141"
lastModifiedBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
lastModifiedDate:1469606019000
message:"twrey"
1:{id: "350fd2bd-f452-495c-9bd5-79aa7c799d02", createdDate: 1469619161000,…}
createdBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
createdDate:1469619161000
id:"350fd2bd-f452-495c-9bd5-79aa7c799d02"
lastModifiedBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
lastModifiedDate:1469619161000
message:"yuo"
我想将json的文件数组推送到json的现有comments数组中,json的结果应如下所示:
comments:[{id: "3124fac5-9d3e-4fa9-8a80-10f626fbf141", createdDate: 1469606019000,…},…]
0:{id: "3124fac5-9d3e-4fa9-8a80-10f626fbf141", createdDate: 1469606019000,…}
createdBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
createdDate:1469606019000
id:"3124fac5-9d3e-4fa9-8a80-10f626fbf141"
lastModifiedBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
lastModifiedDate:1469606019000
message:"twrey"
files:[]
1:{id: "350fd2bd-f452-495c-9bd5-79aa7c799d02", createdDate: 1469619161000,…}
createdBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
createdDate:1469619161000
id:"350fd2bd-f452-495c-9bd5-79aa7c799d02"
lastModifiedBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
lastModifiedDate:1469619161000
message:"yuo"
files:[]
答案 0 :(得分:0)
你只需要向jsonObject添加一个属性即可。哟只需要知道如何 Add new attribute (element) to JSON object using JavaScript
yourCurrentJSONObject.propertyNameToAdd = yourFilesArray;
实施例
var yourCurrentJSONObject = {
comments:[{id: "3124fac5-9d3e-4fa9-8a80-10f626fbf141", createdDate: 1469606019000,…},…]
0:{id: "3124fac5-9d3e-4fa9-8a80-10f626fbf141", createdDate: 1469606019000,…}
createdBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
createdDate:1469606019000
id:"3124fac5-9d3e-4fa9-8a80-10f626fbf141"
lastModifiedBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
lastModifiedDate:1469606019000
message:"twrey"
1:{id: "350fd2bd-f452-495c-9bd5-79aa7c799d02", createdDate: 1469619161000,…}
createdBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
createdDate:1469619161000
id:"350fd2bd-f452-495c-9bd5-79aa7c799d02"
lastModifiedBy:{id: "cf2829b7-0c76-4a08-9562-ccbfa012ef2d", createdDate: 1469605771000, name: "user",…}
lastModifiedDate:1469619161000
message:"yuo"
}
var yourFilesArray = [....];
yourCurrentJSONObject['files'] = yourFilesArray;
// or simply
yourCurrentJSONObject.files = yourFilesArray;
如果您可以按照上述说明操作数组,您将会看到,它包含文件数组alogwith comments和消息
答案 1 :(得分:0)
如果您有两个对象数组并且想要合并它们,我会选择Array.map()和Object.assign()。
当然这两个数组必须具有相同的长度
var a = [{id: 1, asd: 2}, {id: 2, asd: 3}];
var b = [{files: 'asd'}, {files: 'dsa'}];
var c = a.map((obj, i) => Object.assign(obj, b[i]));
console.log(c)