-3
export class Prices{
value : string;
}

const PRICES : Prices[] =[
{value :'$10,000'},{value :'$20,000'},{value :'$30,000'},{value                 
:'$40,000'},{value :'$50,000'},
{value :'$60,000'},{value :'$70,000'},{value :'$80,000'},{value 
:'$90,000'},{value :'$100,000'},
{value :'$125,000'},{value :'$150,000'},{value :'$175,000'},{value 
:'$200,000'},{value :'$225,000'},
{value :'$250,000'},{value :'$275,000'},{value :'$300,000'},{value 
:'$325,000'},{value :'$350,000'},
{value :'$375,000'},{value :'$400,000'},{value :'$425,000'},{value 
:'$450,000'},{value :'$475,000'},
{value :'$500,000'},{value :'$525,000'},{value :'$550,000'},{value 
:'$575,000'},{value :'$600,000'}]


export class ConstantProperty implements OnInit{
prices=PRICES;
selectedPriceMin : Prices;
}

I am selecting the price from a drop down menu in html, Now I want to slice that selected price in order to remove dollar sign.

  • What have you tried to remove the dollar sign? – t.niese Jul 05 '17 at 16:12
  • Possible duplicate of [convert money format into numbers](https://stackoverflow.com/questions/8654456/convert-money-format-into-numbers) – t.niese Jul 05 '17 at 16:27
  • The answer to this question is available on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice), but this is probably not the right question. What you should be asking is, "What is the best way to parse currency in a locale independent way?" – jpaugh Jul 05 '17 at 23:12

5 Answers5

0

To remove a dollar sign (or any character) from the front of a string, do

var withoutDollarSign = withDollarSign.substr(1)
jpaugh
  • 6,634
  • 4
  • 38
  • 90
skiilaa
  • 1,212
  • 11
  • 20
0

Another example: "$550,000".replace("$","");

That results in "550,000"

terpinmd
  • 1,014
  • 8
  • 15
0

Slicing a string is easy as:

"$400.00".slice(1);

or

var currencyString = "$400.00";
var valueString = mystring.slice(1);

Where the 1 is the index the character you want to slice at.

This assumes that the $ sign will always be at the 1st position.

Steak Overflow
  • 7,041
  • 1
  • 37
  • 59
0

If {value :'$10,000'} is the structure of your data, then, you could remove the $ sign in this way:

const data = [{value :'$10,000'},{value :'$20,000'},{value :'$30,000'},{value:'$40,000'},{value :'$50,000'},
{value :'$60,000'},{value :'$70,000'},{value :'$80,000'},{value 
:'$90,000'},{value :'$100,000'},
{value :'$125,000'},{value :'$150,000'},{value :'$175,000'},{value 
:'$200,000'},{value :'$225,000'},
{value :'$250,000'},{value :'$275,000'},{value :'$300,000'},{value 
:'$325,000'},{value :'$350,000'},
{value :'$375,000'},{value :'$400,000'},{value :'$425,000'},{value 
:'$450,000'},{value :'$475,000'},
{value :'$500,000'},{value :'$525,000'},{value :'$550,000'},{value 
:'$575,000'},{value :'$600,000'}];


console.log(
  data.map(i => Object.assign({}, i, {value: i.value.substr(1)}))
)
Hitmands
  • 13,491
  • 4
  • 34
  • 69
-1

You can map the array to apply replace on each item.

console.log(
[{value :'$10,000', region: 'NY'},{value :'$600,000', region: 'NY'}]
    .map(({value,...rest}) => ({value: value.replace('$', ''), ...rest}))
);
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • this loses all the other `x` possible properties. – Hitmands Jul 05 '17 at 16:26
  • @Hitmands updated to hadle that case, but this code can be harder for OP to understand as it introduces more concepts. – sabithpocker Jul 05 '17 at 16:34
  • you are using `Object spread operator` which is not standard, basically my same answer... And don't assume the OP as junior, because it is not needed in this context. – Hitmands Jul 05 '17 at 16:36
  • @Hitmands The code is in `Typescript`, I keep using object spread operator in Typescript as it appears less verbose to me. Again, its more of preferences and environment. – sabithpocker Jul 05 '17 at 16:42
  • no, it is not. Object Spread Operator is under ecmascript proposal, this isn't standard and may be never released. https://github.com/tc39/proposal-object-rest-spread – Hitmands Jul 05 '17 at 16:47
  • @Hitmands I do agree with your concern that it is stage3, but I hope it is fine to use it with TypeScript or Babel. Check an example and disclaimer [here](http://redux.js.org/docs/recipes/UsingObjectSpreadOperator.html). If you can convince redux team and react native team, I'll consider too. Also it is safer to assume `Object spread` may confuse OP when OP is asking for a simple string operation, the original intention being not to dilute the answer by assuming requirements. `assign` and `substring` not the same as `spread` and `replace`.IMHO a similar answer is not offensive in SO. – sabithpocker Jul 06 '17 at 08:00