1

As you can see in the code sample below, in hopes of getting the sum of all the option_msrp values.

var obj, i, sum;
obj = JSON.parse(socookie);
for(i=0; i<obj.length; i++){
    sum += parseInt((obj[i].option_msrp).slice(1));
}

option_msrp is a string in the following format: "$300" (a dollar sign followed by the dollar value, which is a whole number). I'm trying to convert this string into an integer via parseInt.

I'm trying to remove the dollar sign from the string to be able to convert this string into an integer.

I've tried the various methods to do this below and none of them have worked:

//doesn't work 
sum += parseInt((obj[i].option_msrp).slice(1));

//also doesn't work
sum += parseInt((obj[i].option_msrp).replace("$",""));

What am I doing wrong?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
MarisolFigueroa
  • 757
  • 1
  • 14
  • 31
  • Why does it not work ? Debug - Output in console to see why it fails – Weedoze Apr 11 '17 at 13:22
  • 1
    There's something else at fault here. Open the console in your browser (usually F12 will do it) and copy/paste... `parseInt("$300".replace("$", ""));` – Reinstate Monica Cellio Apr 11 '17 at 13:22
  • 1
    This has nothing to do with JSON, so I've removed the tag. Separately: *"I'm iterating through some JSON objects"* It's JSON *before* you parse it. But the time you're looping through the objects, they're just objects, nothing to do with JSON. JSON is a *textual notation* for data exchange [(more)](http://stackoverflow.com/a/2904181/157247). – T.J. Crowder Apr 11 '17 at 13:24
  • 1
    Provide an actual sample for value of `socookie` as per [mcve] – charlietfl Apr 11 '17 at 13:24
  • May I suggest using a library that makes dealing with currency trivial? http://openexchangerates.github.io/accounting.js/ – Waxi Apr 11 '17 at 13:25
  • try parseInt(substr(obj[i].option_msrp,1)) – Nikkorian Apr 11 '17 at 13:26
  • @Nikkorian: Since `parseInt("$300".slice(1))` demonstrably **does** work, how the OP is doing the number parsing skipping the `$` is clearly not the problem. – T.J. Crowder Apr 11 '17 at 13:29

1 Answers1

2

The main problem I'm seeing is that you never initialize sum. Consequently, += parseInt((obj[i].option_msrp).slice(1)); will just leave sum set to NaN (because undefined + anyNumber is NaN). sum will then continue to be NaN on subsequent iterations because NaN + anyNumber is also NaN.

Just set sum = 0 before the loop.

Example of the problem:

var socookie = '[{"option_msrp": "$300"},{"option_msrp": "$100"}]';
var obj, i, sum;
obj = JSON.parse(socookie);
for(i=0; i<obj.length; i++){
    sum += parseInt((obj[i].option_msrp).slice(1));
}
console.log(sum);

Example of the solution:

var socookie = '[{"option_msrp": "$300"},{"option_msrp": "$100"}]';
var obj, i, sum = 0;  // <======
obj = JSON.parse(socookie);
for(i=0; i<obj.length; i++){
    sum += parseInt((obj[i].option_msrp).slice(1));
}
console.log(sum);

Side note: You can get rid of that loop by using Array#reduce. Summing is one of its banner use cases:

var sum = obj.reduce(function(s, e) {
  return s + parseInt(e.option_msrp.slice(1));
}, 0);

var socookie = '[{"option_msrp": "$300"},{"option_msrp": "$100"}]';
var obj = JSON.parse(socookie);
var sum = obj.reduce(function(s, e) {
  return s + parseInt(e.option_msrp.slice(1));
}, 0);
console.log(sum);

Or with ES2015+ (aka "ES6"+) syntax:

const sum = obj.reduce((s, e) => s + parseInt(e.option_msrp.slice(1)), 0);

const socookie = '[{"option_msrp": "$300"},{"option_msrp": "$100"}]';
const obj = JSON.parse(socookie);
const sum = obj.reduce((s, e) => s + parseInt(e.option_msrp.slice(1)), 0);
console.log(sum);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875