最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Modify object values in an array of objects - Stack Overflow

programmeradmin0浏览0评论

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.

Share Improve this question edited Nov 1, 2018 at 9:46 Dave Kalu asked Nov 1, 2018 at 9:43 Dave KaluDave Kalu 1,5955 gold badges22 silver badges43 bronze badges 4
  • Why your tax values are strings(not numbers)? – Mohammad Usman Commented Nov 1, 2018 at 9:45
  • do you want a new array with new objects or just the same with mutated props? – Nina Scholz Commented Nov 1, 2018 at 9:45
  • @MohammadUsman My bad. That was typo. Thanks. – Dave Kalu Commented Nov 1, 2018 at 9:46
  • @NinaScholz A new array would be preferable. – Dave Kalu Commented Nov 1, 2018 at 9:47
Add a ment  | 

6 Answers 6

Reset to default 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);

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

发布评论

评论列表(0)

  1. 暂无评论