admin管理员组

文章数量:1399839

Here I have an array.It is an array of time.

time = ["19.00","19.01","19.34","19.37","20.05","20.12","10.54","11.12"];

I wanted to remove all these quotes.

time = [19.00,19.01,19.34,19.37,20.05,20.12,10.54,11.12];

Actually, I'm getting all these data from Redis as an array.

And passing data to javascript.

var httptime1 = '<?php echo json_encode($httptime1); ?>';

i used to remove quotes this way,

for (var i = 0; i < time1.length; i++)
{
    time1[i] = time1[i].replace(/"/g, " ");
}

But not working.

Can you suggest me, which is the best way?

Here I have an array.It is an array of time.

time = ["19.00","19.01","19.34","19.37","20.05","20.12","10.54","11.12"];

I wanted to remove all these quotes.

time = [19.00,19.01,19.34,19.37,20.05,20.12,10.54,11.12];

Actually, I'm getting all these data from Redis as an array.

And passing data to javascript.

var httptime1 = '<?php echo json_encode($httptime1); ?>';

i used to remove quotes this way,

for (var i = 0; i < time1.length; i++)
{
    time1[i] = time1[i].replace(/"/g, " ");
}

But not working.

Can you suggest me, which is the best way?

Share Improve this question asked May 16, 2016 at 7:29 CodingalienCodingalien 3,0472 gold badges24 silver badges32 bronze badges 4
  • 1 the time that is being returned is array of string..you can simply map them to double or float – rock321987 Commented May 16, 2016 at 7:31
  • check if it's a number using isNaN() and use parseInt() or parseFloat() to convert it to a number. – Rudra Commented May 16, 2016 at 7:33
  • If you care at all about the accuracy of those numbers, you'll want to keep them as a string instead of using inaccurate floating point numbers. – deceze Commented May 16, 2016 at 7:44
  • You can simply make noQuoteTime = time.map(e => e*1) – Redu Commented May 16, 2016 at 9:15
Add a ment  | 

4 Answers 4

Reset to default 5

You don't have quotes in the elements of the JavaScript array. What you have is an array of strings when what you want is an array of numbers.

A solution would be to do the conversion in JavaScript:

var httptime1 = <?php echo json_encode($httptime1); ?>.map(Number);

But the cleanest one would probably be to convert at the source:

var httptime1 = <?php echo json_encode(array_map('floatval', $httptime1)); ?>;

EDIT:

Those look more like hours:minutes than like some real numbers... Which means numbers are useless. Don't you simply want

var httptime1 = <?php echo json_encode($httptime1); ?>;

(note that I removed the single quotes which prevented the parsing as a JS literal array in your code)

Use Number constructor/wrapper object as a callback for Array.map function(it will convert each array item into a number):

var time = ["19.00","19.01","19.34","19.37","20.05","20.12","10.54","11.12"],
    time_new = time.map(Number);

console.log(time_new); // [19, 19.01, 19.34, 19.37, 20.05, 20.12, 10.54, 11.12]

Are these actual quotes or are they strings and you want them to be floats? Because then all you would do is just do a string to number conversion.

I tried using Number function which change string to number.

var time = ["19.00","19.01","19.34","19.37","20.05","20.12","10.54","11.12"];
time.forEach(function(a,i){time[i]=Number(a)});  
console.log(time);

本文标签: phpHow to remove quotes from an array in javascriptStack Overflow