I have an object like this:
"params": {
"name": "Alexa",
"lastName": "Simpson",
"city": "London"
}
Here’s the request that I need to implement: https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate
I want to create an array of objects and pass the key of my object to the value of "text". for example "name" goes to {{text1}} and "Alexa" to "replaceText1" dynamically.
{
"requests": [
{
"replaceAllText": {
"containsText": {
"text": "{{text1}}",
"matchCase": "true"
},
"replaceText": "replaceText1"
}
},
{
"replaceAllText": {
"containsText": {
"text": "{{value2}}",
"matchCase": "true"
},
"replaceText": "replaceText2"
}
}
]
}
2
Answers
You can
map
theObject.entries
ofobj.param
to create a request object for each key/value pair, then place that as the value in an object with a key ofrequests
:Try this out. This uses
Object.entries
to get thekey
,value
pair and they are used as a resulting objects. You can parse theparams
object usingJSON.parse
.Use
JSON.stringify
to convert to the resulting string.