关于javascript:使用js从现有数据中删除数字中的逗号

Using js remove comma from number from existing data

本问题已经有最佳答案,请猛点这里访问。

数组数据是

1
2
3
4
5
6
7
8
9
10
[{
 "Date":"2018-03-20",
 "Total":"10,459"
}, {
 "Date":"2018-03-21",
 "Total":"11,947"
}, {
 "Date":"2018-03-22",
 "Total":"12,932",
}];

我需要像这样:

1
2
3
4
5
6
7
8
9
10
[{
 "Date":"2018-03-20",
 "Total":"10459"
}, {
 "Date":"2018-03-21",
 "Total":"11947"
}, {
 "Date":"2018-03-22",
 "Total":"12932",
}];


使用jquery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var data=[{
 "Date":"2018-03-20",
 "Total":"10,459"
}, {
 "Date":"2018-03-21",
 "Total":"11,947"
}, {
 "Date":"2018-03-22",
 "Total":"12,932",
}];
$.each(data,function(index,item){
  item.Total=item.Total.replace(",","");
   console.log(item);
});

1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">

使用$.each迭代对象。


您可以使用map函数和replace函数从Total中删除逗号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let data = [{
 "Date":"2018-03-20",
 "Total":"10,459"
}, {
 "Date":"2018-03-21",
 "Total":"11,947"
}, {
 "Date":"2018-03-22",
 "Total":"12,932",
}];

let res = data.map(elem => {
  elem.Total = elem.Total.replace(',', '');
  return elem;
})

console.log(res);


简单的解决方案是使用parsefloat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 const data = [{
     "Date":"2018-03-20",
     "Total":"10,459"
    }, {
     "Date":"2018-03-21",
     "Total":"11,947"
    }, {
     "Date":"2018-03-22",
     "Total":"12,932",
    }];  

      data.map(row => {
          row.Total = parseFloat(row.Total.replace(/,/g, ''));
        })


You can use .map for this and inside its callback replace the comma

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const x = [{
 "Date":"2018-03-20",
 "Total":"10,459"
}, {
 "Date":"2018-03-21",
 "Total":"11,947"
}, {
 "Date":"2018-03-22",
 "Total":"12,932",
}];

const y = x.map(el => {
  el.Total = el.Total.replace(/,/g,"");
  return el;
})

console.log(y);


使用Array.prototype.map()和string.prototype.replace()。修改Total属性:

[cc lang="javascript"]var data = [{"Date":"2018-03-20","Total":"10,459