简体   繁体   English

修改对象数组中的对象值

[英]Modify object values in an array of objects

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: 我尝试使用map函数,如下所示:

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] 但是我得到的数组只有Math.round的结果,而不是得到一个经过修改的对象数组,如下所示: [ 123.34, 3243.12, 122.34]

How do I map the array of objects to get the expected result as described above. 如何映射对象数组以获得如上所述的预期结果。

Thanks. 谢谢。

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 劳埃德

You can update tax in your map function. 您可以在map功能中更新tax

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

Array.map processes the entry in array and return the processed value. Array.map处理数组中的条目并返回已处理的值。 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); 

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

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. map数组并使用新的税值返回每个对象,该税值已转换为固定为两位小数的浮点数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM