I don’t figure it out, how to clear each value of each key inside an object.
The result should be like this.
JavaScript
x
4
1
const initialObject = { a: "valueA", b: "valueB", c: "valueC" };
2
3
const finalObject = { a: "", b: "", c: "" };
4
I’m using Typescript.
Thank you for your help.
Advertisement
Answer
Map the keys to an array of [key, ""]
and then convert to an object using Object.fromEntries()
:
JavaScript
1
8
1
const initialObject = {a: "valueA", b: "valueB", c: "valueC"}
2
3
const finalObject = Object.fromEntries(
4
Object.keys(initialObject)
5
.map(key => [key, ""])
6
)
7
8
console.log(finalObject)