Skip to content
Advertisement

Clear each value of each key inside an object

I don’t figure it out, how to clear each value of each key inside an object.

The result should be like this.

const initialObject = { a: "valueA", b: "valueB", c: "valueC" };

const finalObject = { a: "", b: "", c: "" };

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():

const initialObject = {a: "valueA",  b: "valueB", c: "valueC"}
        
const finalObject = Object.fromEntries(
  Object.keys(initialObject)
    .map(key => [key, ""])
)

console.log(finalObject)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement