how convert values string into a integer object array by key

Viewed 39

i have this object array:

let arr = [
    {
        'pippo': '1',
    'descrizione': 'ciao'
    }
];

and i want convert "1" to 1 by key:

let arr = [
        {
            'pippo': 1,
        'descrizione': 'ciao'
        }
    ];

any solution?

br Max

1 Answers

You can iterate over the objects and create a new array with the converted number with the help of parseInt().

arr = arr.map(item => { 
   return {
      ...item, //copies all items first...
      pippo: parseInt(item.pippo) //...then overwrites pippo
   }
}
Related