I have a dynamic array which can have n number of sections/fields as below.
[
{
"id": "section-1",
"fields": [{
"id": "field1",
"value": "A1"
}, {
"id": "field2",
"value": null
}
]
}, {
"id": "section-2",
"fields": [{
"id": "field3",
"value": null
}, {
"id": "field4",
"value": null
}
]
}, {
"id": "section-3",
"fields": [{
"id": "field5",
"value": "A5"
}
]
}
]
Can I use ES6 to update any null values to empty string in this array. So the output would be the same exact structure replacing any "value": null
with "value": ""
2
Answers
Nested
forEach()
calls:You can use a pretty simple neat trick using
JSON.parse
andJSON.stringify
usingreviver parameter
as: