For example I have this Array:
const arr = [
{val1: '123', text: 'not a num', new:'123.34'},
{val1: '123', text: 'not a number', new:'123.34'},
{val1: '123', text: 'yahoo', new:'123.34'}
]
And I want to convert the keys val1 and new into a number,
so I created this function:
const convertToNumbers = (arr: Array<{ unknown }>, keys: Array<string>) => {
return arr.map((val) => {
keys.map((k) => {
console.log(val[k]);
return (val[k] = +val[k]);
});
});
};
Expected result should be like this:
const result = [
{val1: 123, text: 'not a num', new:123.34},
{val1: 123, text: 'not a number', new:123.34},
{val1: 123, text: 'yahoo', new:123.34}
]
The function will accept both an Array(first param is the Array itself and the second contains the key to be converted into number).
Is there a way to solve my problem?
parseIntorparseFloatto do the conversion.map()(keys.map((k) => ...)) then.map()is the wrong tool