-2

The array data is

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

And I need to be like this:

[{
  "Date": "2018-03-20",
  "Total": "10459"
}, {
  "Date": "2018-03-21",
  "Total": "11947"
}, {
  "Date": "2018-03-22",
  "Total": "12932",
}];
str
  • 42,689
  • 17
  • 109
  • 127
  • 2
    Please show us what have you tried so far. Are you stuck on some specific step when implementing this? – Ionut Necula Oct 16 '18 at 11:25
  • @lonut i have only this json the things i done its fine now what i have to do is to remove comma from number inside total. – Prakash Pandey Oct 16 '18 at 11:28
  • https://www.google.nl/search?q=remove+comma+json+values+javascript+site:stackoverflow.com – mplungjan Oct 16 '18 at 11:29
  • Since it's a string, just use a simple `.replace()`. For the record, this is a good example of why we save integers as actual integers instead of some language based string, since doing that would avoid this entire problem. ( and probably other problems not asked about ) – Shilly Oct 16 '18 at 11:31

5 Answers5

0

Use Array.prototype.map() and String.prototype.replace() to modify the property 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);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

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

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);
void
  • 36,090
  • 8
  • 62
  • 107
0

Simple solution would be to use 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, ''));
        })
Bharata
  • 13,509
  • 6
  • 36
  • 50
Dinosan0908
  • 1,082
  • 2
  • 8
  • 19
0

You can use the map function and the replace function to remove the commas from the 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);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Using 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>

use $.each to iterate over an object.

Sumesh TG
  • 2,557
  • 2
  • 15
  • 29