javascript中的字符串切片

时间:2017-07-05 16:11:20

标签: javascript html angular

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

我从html的下拉菜单中选择价格, 现在我想切割选定的价格以便删除美元符号。

5 个答案:

答案 0 :(得分:0)

要从字符串的前面删除美元符号(或任何字符),请执行

var withoutDollarSign = withDollarSign.substr(1)

答案 1 :(得分:0)

另一个例子:"$550,000".replace("$","");

这导致“550,000”

答案 2 :(得分:0)

切割字符串非常简单:

"$400.00".slice(1);

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

其中1是您要切片的字符的索引。

这假设$符号始终位于第1位。

答案 3 :(得分:0)

如果{value :'$10,000'}是您数据的结构,那么, 你可以用这种方式删除$标志:



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




答案 4 :(得分:-1)

您可以map数组在每个项目上应用replace



console.log(
[{value :'$10,000', region: 'NY'},{value :'$600,000', region: 'NY'}]
    .map(({value,...rest}) => ({value: value.replace('$', ''), ...rest}))
);