0

I'm doing the following to capitalize all keys of an object response:

getBudget: build.query<BudgetType, void>({
    query: () => "budget",
    providesTags: ["Budget"],
    transformResponse: (response: BudgetType) => {
        const deepCopy = {...response};
        Object.keys(deepCopy).forEach((key) => key[0].toUpperCase() + key.slice(1));
        console.log(deepCopy);
        return deepCopy;
    }
}),

Budget type:

export type BudgetType = {
    bills: number | null ,
    transportation: number | null ,
    health: number | null ,
    school: number | null ,
    food: number | null ,
    entertainment: number | null 
}

But it's not working, unsure why

Mathew
  • 318
  • 11
  • What does this have to do with RTK, Real Time Kinematics? – Barmar Jul 25 '23 at 17:45
  • `map()` returns an array of the results of the callback. You're not assigning that result anywhere. It doesn't modify the object in place. – Barmar Jul 25 '23 at 17:47
  • I tried forEach as well, nothing changed. – Mathew Jul 25 '23 at 17:49
  • That doesn't modify in place, either. You can't change strings in place, you need to create a new property: `deepCopy[key[0].toUpperCase() + key.slice(1)] = deepCopy[key]` – Barmar Jul 25 '23 at 17:54

0 Answers0