Programmer
Programmer

Reputation: 8717

Copy object using 'extend' turns the integer property to string

I have an object:

var seriesOptions = {
y: parseFloat($(this).find('y').text()).toFixed(2),
color: colors[index],
level : dlevel
};

Where I am parsing and assigning value via reading a XML tree structure:

<series> <!-- $this--><y>55.34</y></series>

Below the code I make a copy of the object

series = $.extend(true, {}, seriesOptions);

The issue I am facing is that the "y" property gets converted from 55.34 to "55.34" (converted to string type) which makes my code behaving faulty. Is there a way I can copy seriesOptions.y to series.y as integer itself?

Upvotes: 0

Views: 152

Answers (3)

Christoph
Christoph

Reputation: 51201

Since toFixed() returns a string you could either say parseFloat() afterwards or start with an operation that converts the string to number automatically.

Upvotes: 0

Engineer
Engineer

Reputation: 48793

You could use

Math.round(parseFloat($(this).find('y').text())*100)/100

Instead of

parseFloat($(this).find('y').text()).toFixed(2)

And your 'y' will be number, not string.

Upvotes: 1

SLaks
SLaks

Reputation: 887479

.toFixed(2) returns a string, not a number.

Upvotes: 2

Related Questions