I have an array of objects that looks like this:
const arr1 = [
{id: 1, name: 'Dave', tax: 123.34543}
{id: 2, name: 'John', tax: 3243.12323}
{id: 3, name: 'Tom', tax: 122.34324}
]
And I am trying to round off the tax value, so in the end the array should look like this:
[
{id: 1, name: 'Dave', tax: 123.34}
{id: 2, name: 'John', tax: 3243.12}
{id: 3, name: 'Tom', tax: 122.34}
]
I tried using the map
function like so:
arr1.map(value => Math.round(value.tax * 100)/100);
but instead of getting a modified array of objects, I get an array with only the result of the Math.round
which looks like this:
[ 123.34, 3243.12, 122.34]
How do I map the array of objects to get the expected result as described above.
Thanks.
I have an array of objects that looks like this:
const arr1 = [
{id: 1, name: 'Dave', tax: 123.34543}
{id: 2, name: 'John', tax: 3243.12323}
{id: 3, name: 'Tom', tax: 122.34324}
]
And I am trying to round off the tax value, so in the end the array should look like this:
[
{id: 1, name: 'Dave', tax: 123.34}
{id: 2, name: 'John', tax: 3243.12}
{id: 3, name: 'Tom', tax: 122.34}
]
I tried using the map
function like so:
arr1.map(value => Math.round(value.tax * 100)/100);
but instead of getting a modified array of objects, I get an array with only the result of the Math.round
which looks like this:
[ 123.34, 3243.12, 122.34]
How do I map the array of objects to get the expected result as described above.
Thanks.
tax
values are strings(not numbers)?
– Mohammad Usman
Commented
Nov 1, 2018 at 9:45
You can update tax
in your map
function.
See implementation below.
const arr1 = [
{id: 1, name: 'Dave', tax: '123.34543'},
{id: 2, name: 'John', tax: '3243.12323'},
{id: 3, name: 'Tom', tax: '122.34324'},
];
const taxRoundedArray = arr1.map(item => {
let tax = Math.round(item.tax * 100)/100
return {
...item,
tax
}
});
console.log(taxRoundedArray);
You could map new objects with the wanted values.
const
array = [{ id: 1, name: 'Dave', tax: 123.34543 }, { id: 2, name: 'John', tax: 3243.12323 }, { id: 3, name: 'Tom', tax: 122.34324 }],
result = array.map(o => Object.assign({}, o, { tax: Math.round(o.tax * 100) / 100 }));
console.log(result);
You are very close to the correct solution, see below:
arr1.map(value => {
value.tax = Math.round(value.tax * 100)/100);
return value
});
You need to return the altered object otherwise it gets overwritten.
Hope this helps
Lloyd
Array.map processes the entry in array and return the processed value. In the attempt, you were only returning the updated tax, however, you will need to return the object. Try following
const arr1 = [{id: 1, name: 'Dave', tax: 123.34543},{id: 2, name: 'John', tax: 3243.12323},{id: 3, name: 'Tom', tax: 122.34324}];
const arr2 = arr1.map(({tax, ...rest}) => ({...rest, tax: Math.round(tax * 100)/100}));
console.log(arr2);
map
over the array and return each object with a new tax value that has been turned to a floating-point number fixed to two decimal places.
const arr1 = [{"id":1,"name":"Dave","tax":"123.34543"},{"id":2,"name":"John","tax":"3243.12323"},{"id":3,"name":"Tom","tax":"122.34324"}];
const arr2 = arr1.map(obj => {
const tax = +Number.parseFloat(obj.tax).toFixed(2);
return { ...obj, tax };
})
console.log(arr2);
You can do:
const arr1 = [
{id: 1, name: 'Dave', tax: '123.34543'},
{id: 2, name: 'John', tax: '3243.12323'},
{id: 3, name: 'Tom', tax: '122.34324'}
];
const result = arr1.map(user => {
user.tax = (Math.round(+user.tax * 100) / 100);
return user;
});
console.log(result);