3

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.

4
  • Why your tax values are strings(not numbers)? Commented Nov 1, 2018 at 9:45
  • do you want a new array with new objects or just the same with mutated props? Commented Nov 1, 2018 at 9:45
  • @MohammadUsman My bad. That was typo. Thanks. Commented Nov 1, 2018 at 9:46
  • @NinaScholz A new array would be preferable. Commented Nov 1, 2018 at 9:47

6 Answers 6

3

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);

Sign up to request clarification or add additional context in comments.

4 Comments

This adds an extra tax property to each object.
It shouldn't do. Look at the returned array. As it uses the spread operator it hasn't added another tax property. medium.com/front-end-hacking/…
also, just so you are aware, the solution you accepted is more or less the same, but has used Object.assign instead of the spread operator, but they do the same thing.
Oh I see. I believe I made an error on my end when I was testing your solution. Thanks for the help nonetheless.
1

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);

Comments

1

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

2 Comments

Ahh I see. Stupid me. Thanks.
this approach alters the original object, becaus it assigns a new value to tax. please look to arr1.
1

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);

Comments

0

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);

Comments

0

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);

2 Comments

I'm curious to know what the + is used for?
That is the Unary plus (+) operator to convert string into a number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.