-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",
}];
4
  • 2
    Please show us what have you tried so far. Are you stuck on some specific step when implementing this? Commented Oct 16, 2018 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. Commented Oct 16, 2018 at 11:28
  • google.nl/… Commented Oct 16, 2018 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 ) Commented Oct 16, 2018 at 11:31

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Your json is not json, it is just a javascript object array
@mamun thanks it works :)
@PrakashPandey, you are most welcome :)
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);

Comments

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

Comments

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.

1 Comment

$.each() => data.forEach() JQuery isn't mentioned anywhere in the question.
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, ''));
        })

1 Comment

row.Total = +(row.Total.replace(/,/g, ''));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.