0

I need to amend the amount to add £ before the number and change it to two decimals i.e. 9ドル.00

const data = [
 {
 title: 'Todo',
 amount: 9.99,
 },
 {
 title: 'In-Progress',
 amount: 4,
 },
 {
 title: 'Completed',
 amount: 10,
 },
 {
 title: 'Short',
 amount: 15.48,
 },
];

I can't work out how?

asked Feb 3, 2023 at 10:06
2
  • are you just asking how to read and set properties of objects inside an array? anyway in general Number.toFixed(2) toFixed Commented Feb 3, 2023 at 10:08
  • Does this answer your question? Format number to always show 2 decimal places Commented Feb 3, 2023 at 10:17

3 Answers 3

1

You could do that, or you could use the Intl.NumberFormat to change it to currency on-the-fly. That way you get to keep your data neutral, and let the browser/JS handle the transformation only when it's needed.

const data=[{title:"Todo",amount:9.99},{title:"In-Progress",amount:4},{title:"Completed",amount:10},{title:"Short",amount:15.48}];
const style = { style: 'currency', currency: 'GBP' };
for (const obj of data) {
 const currency = new Intl.NumberFormat('en-GB', style).format(obj.amount);
 console.log(currency);
}

If you still want to change the data this method is still preferable. map over the data, destructure the amount from the object, and return a new object with the transformed amount.

const data=[{title:"Todo",amount:9.99},{title:"In-Progress",amount:4},{title:"Completed",amount:10},{title:"Short",amount:15.48}];
const style = { style: 'currency', currency: 'GBP' };
function toGBP(amount) {
 return new Intl.NumberFormat('en-GB', style).format(amount);
}
const out = data.map(obj => {
 const { amount, ...rest } = obj;
 return { ...rest, amount: toGBP(amount) };
});
console.log(out);

Additional documentation

answered Feb 3, 2023 at 10:14
Sign up to request clarification or add additional context in comments.

Comments

0

Just map through it and use .toFixed(2) for the decimal.

const data = [
 {
 title: 'Todo',
 amount: 9.99,
 },
 {
 title: 'In-Progress',
 amount: 4,
 },
 {
 title: 'Completed',
 amount: 10,
 },
 {
 title: 'Short',
 amount: 15.48,
 },
];
const mapped = data.map((n) => { return {title: n.title, amount: `£${n.amount.toFixed(2)}`}});
console.log(mapped);

answered Feb 3, 2023 at 10:11

Comments

0

You can use Number#toFixed(2) and Array#map() methods as follows:

const 
 data = [ { title: 'Todo', amount: 9.99, }, { title: 'In-Progress', amount: 4, }, { title: 'Completed', amount: 10, }, { title: 'Short', amount: 15.48 } ],
 
 out = data.map(({title,amount}) => ({title,amount:`£${amount.toFixed(2)}`}));
 
console.log( out );

answered Feb 3, 2023 at 10:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.