使用js从现有数据的数字中删除逗号

时间:2018-10-16 11:23:50

标签: javascript

数组数据为

[{
  "Date": "2018-03-20",
  "Total": "10,459"
}, {
  "Date": "2018-03-21",
  "Total": "11,947"
}, {
  "Date": "2018-03-22",
  "Total": "12,932",
}];

我需要这样:

[{
  "Date": "2018-03-20",
  "Total": "10459"
}, {
  "Date": "2018-03-21",
  "Total": "11947"
}, {
  "Date": "2018-03-22",
  "Total": "12932",
}];

5 个答案:

答案 0 :(得分:0)

使用Array.prototype.map()String.prototype.replace() 来修改属性Total

var data = [{"Date": "2018-03-20", "Total": "10,459"},{"Date": "2018-03-21", "Total": "11,947"},{"Date": "2018-03-22", "Total": "12,932",}];

data = data.map(i => {
  i.Total = i.Total.replace(/,/g,'');
  return i;
});
console.log(data);

答案 1 :(得分:0)

您可以为此使用.map,并在其回调中替换逗号

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

答案 2 :(得分:0)

简单的解决方案是使用parseFloat

 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, ''));
        })

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

使用JQuery

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);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

使用$.each遍历对象。