Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

javascript - Inserting a '£' in between a minus symbol and figure following calculation

I've got the following array:

Array (3) 
    0 {_id: "5fea07cd143fd50008ae1ab2", newBalance: 1500, balanceDate: "2020-12-28T16:29:00.391Z"} 
    1 {_id:"5fea0837b2a0530009f3886f", newBalance: 1115, balanceDate: "2020-12-28T16:30:45.217Z"}
    2 {_id: "5fec30faef904e0dd1e39c60", newBalance: 1415, balanceDate: "2020-12-30T07:49:13.214Z"}

I then have the following function, which maps through this array and picks out the newBalance from each of them. It then takes the previous value and subtracts the current value to work out the difference:

  const diffs = debtBalance.map((item, index) =>
    index === 0 ? 0 : debtBalance[index - 1].newBalance - item.newBalance
  );

This outputs the following:

Array (3)
0 0
1 385
2 -300

What I then want to do is in examples like the second entry in the array (-300), take that value and separate out the '-' and the int, then in between them place a '£' sign.

So then if I log the updated array, the output would look like:

// 0, 385, -£300

I'm fine with the conditionals, this is working and I can pick out the figures, I just need to make the change to figures with a '-' before them and add a '£' in between.

Any feedback would be appreciated! :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When inserting the £ symbol, you need to convert your data type from number to string. So probably you then want to first map all the values in your array to strings (to be consistent). Like arr.map(String) will do that. So that is the first thing to do.

You can then execute a replace on each member in yet another mapping:

 arr.map(s => s.replace("-", "-£"))

And if you want the currency symbol to be in front of the positive amount-strings as well (seems logical), then do it in one go with the previous replacement:

 arr.map(s => s.replace(/d/, "£$&"))

This replaces the first digit with the same digit prefixed with the currency symbol. Here it doesn't matter whether the string has a minus sign or not.

Remark

It seems to me that the first 0 in your diffs array is not very useful, as it will always be zero. I would just omit that value.

So I would only generate two values for an array of three, and if a value is positive I would also prefix it with the currency symbol, in order to be consistent:

const debtBalance = [
   { _id: "5fea07cd143fd50008ae1ab2", newBalance: 1500, balanceDate: "2020-12-28T16:29:00.391Z"}, 
   {_id:"5fea0837b2a0530009f3886f", newBalance: 1115, balanceDate: "2020-12-28T16:30:45.217Z"},
   {_id: "5fec30faef904e0dd1e39c60", newBalance: 1415, balanceDate: "2020-12-30T07:49:13.214Z"}
];

const diffs = debtBalance.slice(1).map((item, index) =>
    item.newBalance - (debtBalance[index - 1]?.newBalance || 0) 
).map(String).map(s => s.replace(/d/, "£$&"));

console.log(diffs);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...