[英]How to add Key/Value in return JSON object in javascript?
I am getting json object as a response.我得到 json 对象作为响应。 I want to add a new key/value pair in that object.
我想在该对象中添加一个新的键/值对。 I tried object.assign but its overriding the value.
我尝试过 object.assign 但它覆盖了该值。 Is there any way to add the new key/value field in response object
有没有办法在响应对象中添加新的键/值字段
{
"0": {
"createdDate": "2021-11-08T19:51:02.000Z",
"description": null,
"id": "0TO7e000000RJMBGA4",
"images": {
"coverImageUrl": null,
"featuredImageUrl": "/customercommunityv4/file-asset/structuresicongoldpng?v=1"
},
"isBeingDeleted": false,
"name": "Structures",
"nonLocalizedName": "Structures",
"talkingAbout": 1,
"url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJMBGA4"
},
"1": {
"createdDate": "2021-11-08T19:49:56.000Z",
"description": null,
"id": "0TO7e000000RJLrGAO",
"images": {
"coverImageUrl": null,
"featuredImageUrl": "/customercommunityv4/file-asset/X3ddesigniconyellowpng?v=1"
},
"isBeingDeleted": false,
"name": "Class3Error",
"nonLocalizedName": "Class3Error",
"talkingAbout": 1,
"url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJLrGAO"
},
"2": {
"createdDate": "2021-11-08T19:50:12.000Z",
"description": null,
"id": "0TO7e000000RJLwGAO",
"images": {
"coverImageUrl": null,
"featuredImageUrl": "/customercommunityv4/file-asset/X202101icondigitaltwingold120x120png?v=1"
},
"isBeingDeleted": false,
"name": "Digital Twin",
"nonLocalizedName": "Digital Twin",
"talkingAbout": 1,
"url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJLwGAO"
},
} }
I want to add 'newUrl' key and its corresponding value in this object after "url" field.我想在“url”字段之后在此对象中添加“newUrl”键及其对应的值。 Like
喜欢
"0": {
"createdDate": "2021-11-08T19:51:02.000Z",
"description": null,
"id": "0TO7e000000RJMBGA4",
"images": {
"coverImageUrl": null,
"featuredImageUrl": "/customercommunityv4/file-asset/structuresicongoldpng?v=1"
},
"isBeingDeleted": false,
"name": "Structures",
"nonLocalizedName": "Structures",
"talkingAbout": 1,
"url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJMBGA4",
"newUrl": "test/112/testname"
},
Thanks in advance提前致谢
You first need to parse your responseData
and then manipulate it.您首先需要解析您的
responseData
,然后对其进行操作。
const newObj = JSON.parse(responseData);
Object.entries(newObj).forEach(([key, value]) => newObj[key] = { ...value, newUrl: '...' });
Or if you only want to add newUrl
to a specific sub-object:或者,如果您只想将
newUrl
添加到特定的子对象:
const newObj = JSON.parse(obj);
newObj[key] = { ...newObj[key], newUrl: '...' };
Then you can JSON.stringify(newObj)
if needed.然后你可以根据需要
JSON.stringify(newObj)
。
Following methods will be used ...将使用以下方法...
Object.assign
Object.entries
(non mutating approach) Object.values
(mutating approach) Object.entries
(非变异方法) Object.values
(变异方法)Array#reduce
(non mutating approach) Array#forEach
(mutating approach) Array#reduce
(非变异方法) Array#forEach
(变异方法) Creating a new augmented object from the existing response data (non mutating approach) ...从现有的响应数据创建一个新的增强对象(非变异方法)......
const responseData = { "0": { "createdDate": "2021-11-08T19:51:02.000Z", "description": null, "id": "0TO7e000000RJMBGA4", "images": { "coverImageUrl": null, "featuredImageUrl": "/customercommunityv4/file-asset/structuresicongoldpng?v=1" }, "isBeingDeleted": false, "name": "Structures", "nonLocalizedName": "Structures", "talkingAbout": 1, "url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJMBGA4" }, "1": { "createdDate": "2021-11-08T19:49:56.000Z", "description": null, "id": "0TO7e000000RJLrGAO", "images": { "coverImageUrl": null, "featuredImageUrl": "/customercommunityv4/file-asset/X3ddesigniconyellowpng?v=1" }, "isBeingDeleted": false, "name": "Class3Error", "nonLocalizedName": "Class3Error", "talkingAbout": 1, "url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJLrGAO" }, "2": { "createdDate": "2021-11-08T19:50:12.000Z", "description": null, "id": "0TO7e000000RJLwGAO", "images": { "coverImageUrl": null, "featuredImageUrl": "/customercommunityv4/file-asset/X202101icondigitaltwingold120x120png?v=1" }, "isBeingDeleted": false, "name": "Digital Twin", "nonLocalizedName": "Digital Twin", "talkingAbout": 1, "url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJLwGAO" } }; const augmentedCopy = Object .entries(responseData) .reduce((result, [key, value]) => Object.assign(result, { [key]: { ...value, newUrl: `test/${ key }/testname` } }), {} ); console.log({ augmentedCopy, responseData })
.as-console-wrapper { min-height: 100%!important; top: 0; }
Altering/mutating the existing response data (mutating approach) ...改变/变异现有的响应数据(变异方法)......
const responseData = { "0": { "createdDate": "2021-11-08T19:51:02.000Z", "description": null, "id": "0TO7e000000RJMBGA4", "images": { "coverImageUrl": null, "featuredImageUrl": "/customercommunityv4/file-asset/structuresicongoldpng?v=1" }, "isBeingDeleted": false, "name": "Structures", "nonLocalizedName": "Structures", "talkingAbout": 1, "url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJMBGA4" }, "1": { "createdDate": "2021-11-08T19:49:56.000Z", "description": null, "id": "0TO7e000000RJLrGAO", "images": { "coverImageUrl": null, "featuredImageUrl": "/customercommunityv4/file-asset/X3ddesigniconyellowpng?v=1" }, "isBeingDeleted": false, "name": "Class3Error", "nonLocalizedName": "Class3Error", "talkingAbout": 1, "url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJLrGAO" }, "2": { "createdDate": "2021-11-08T19:50:12.000Z", "description": null, "id": "0TO7e000000RJLwGAO", "images": { "coverImageUrl": null, "featuredImageUrl": "/customercommunityv4/file-asset/X202101icondigitaltwingold120x120png?v=1" }, "isBeingDeleted": false, "name": "Digital Twin", "nonLocalizedName": "Digital Twin", "talkingAbout": 1, "url": "/services/data/v52.0/connect/communities/0DB7e000000GmlRGAS/topics/0TO7e000000RJLwGAO" } }; Object .values(responseData) .forEach((value, idx) => value.newUrl = `test/${ idx }/testname` // ... OR ... // // Object.assign(value, {newUrl: `test/${ idx }/testname` }) ); console.log({ responseData })
.as-console-wrapper { min-height: 100%!important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.