Hello everyone, I have the following array of objects in their integer values properties, I am trying to transform those integers to strings:
const pension = [{
cat_bank_id: 91,
cat_discount: 1,
clabe: "901",
curp: "ASDA",
name: "asda",
percentage: "12"
}]
The keys that contain the integer are cat_bank_id and cat_discount, how can I achieve this? Thank you very much in advance for your answers and comments.
I am trying the following way, I manage to convert the integer to a string but I don't know how to generate a new object array with the transformed values.
const pension = [{
cat_bank_id: 91,
cat_discount: 1,
clabe: "901",
curp: "ASDA",
name: "asda",
percentage: "12"
}];
console.log(pension)
pension.forEach(pension => {
console.log('sin transformar', typeof pension.cat_bank_id)
const string = String(pension.cat_bank_id)
console.log('transformado', typeof string)
})
The issue is that you can go through the object with for in instead of with forEach, this based on the first example you passed, in case of being an array and inside an object, you should only have the array and its index before the object, you could do something like this if it's only those 2 keys to modify:
Or something like this if you want to modify all integers:
To pass the value to string , you can use both
valor.toString()
asString(valor)
.so you can convert integers to string with the function
toString()
A very simple way to do this is to map your array with Array.map , to apply the toString function on each property of your object. You can access the keys of your object with Object.keys and iterate over them with Array.forEach
If you already know exactly which keys you want to change, it is not necessary to do it on all keys. You should only consider the maintainability concepts of your project.