最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove the first character of a variable - Stack Overflow

programmeradmin1浏览0评论

HI All,

In my js file i am getting an array of records (negetive values ) like this

deletedDetailIds = $.map(eformDetailIds, function(e) { return e < 0 ? e : null; }); 

For example the deletedDetailIds = -1234,-1235,-1256 like this.

Now i have to create an array and add these values with out negetive symbols. like

newArray = 1234,1235,1256.

Please help me in this

HI All,

In my js file i am getting an array of records (negetive values ) like this

deletedDetailIds = $.map(eformDetailIds, function(e) { return e < 0 ? e : null; }); 

For example the deletedDetailIds = -1234,-1235,-1256 like this.

Now i have to create an array and add these values with out negetive symbols. like

newArray = 1234,1235,1256.

Please help me in this

Share Improve this question asked Jun 1, 2011 at 19:34 SatyaSatya 5254 gold badges14 silver badges27 bronze badges 0
Add a ment  | 

9 Answers 9

Reset to default 6

The correct way would be to use Math.abs:

$.map(eformDetailIds, function(e) { 
    return e < 0 ? Math.abs(e) : null; 
});

You could also multiply with -1.

You are dealing with numbers. Only the string representation contains a - to indicate the negative value, so you cannot really remove the first character here.

Easy solution is multiply the values by -1 which make value + for you

example

var a = -12345 * -1;
   a = 12345;

try this:

deletedDetailIds = $.map(eformDetailIds, function(e) { return e < 0 ? Math.abs(e) : null; });

Is this what you're looking for?

deletedDetailIds = $.map(eformDetailIds, function(e) {
    return e < 0 ? Math.abs(e) : null;
})

if u are willing for replace -ve symbol then,

deletedDetailIds.replace(/-/g, "");

Use abs:

So Math.abs(number)

Where number will be the negative number

Try this

deletedDetailIds = $.map(eformDetailIds, function(e) { return e < 0 ? e : null; });
var total = 0;
$.each(deletedDetailIds, function(a, b){
 total += Math.abs(b)
});
    var deletedDetailIds = $.map(eformDetailIds, 
       function(item) { return item < 0 ? -item : null; }
    ); 

Use the Math.abs function:

$.map(eformDetailIds, function(e) { 
        return e && e < 0 ? Math.abs(e) : 0; 
});

More info here

发布评论

评论列表(0)

  1. 暂无评论