-7

I am a bit new in Jquery ...I am plotting highchart graph

I am setting My x Axis catagoris :

chart.axes[0].categories: [1.1,2.0]

I need to place "" on all elements.like ..

chart.axes[0].categories: ["1.1","2.0".....]

I have seen the use of replacewith() method ..But it seems it replace the content of a particular element ..How can I implement it in array ..

I tried this :

 $.each(Arr1, function (index, value) {

            value.replace('','"');
        });

and this one ..

Arr1.push(String(value));

But this is not working :

Any suggestion will be helpful

Liam
  • 27,717
  • 28
  • 128
  • 190
user3767164
  • 161
  • 1
  • 7
  • 19
  • Why do you want to do that? – PeterKA Jul 07 '14 at 17:58
  • `Arr1[index] = value.replace('"',"'");` – nullability Jul 07 '14 at 17:59
  • Whats the point of doing so? If Arr1 is an array. – Satpal Jul 07 '14 at 17:59
  • erm, are you sure it is a string or an array? – hjpotter92 Jul 07 '14 at 17:59
  • I m plotting high chart graph..I need to set x axis ...it can only work with data in single quotes – user3767164 Jul 07 '14 at 18:00
  • `Arr1` is does not seem to be string... It seems to be array. So you can't change what system decides to use... `.replace()` would work if it would be `"Arr1: ...."` – j809 Jul 07 '14 at 18:00
  • 1
    do you have an array of strings with quotes in the strings? or do you want to format how the array is printed? ie: to you have ['"1.1413.2"','"2.0.4.7"','"2.0.4.7"','"2.0.4.7"','"2.0.4.7"'] or just an array of strings that have version numbers in them? – Hurricane Hamilton Jul 07 '14 at 18:00
  • 1
    I edited my question... it is an array .. – user3767164 Jul 07 '14 at 18:01
  • I think what you want is [`xAxis.categories`](http://api.highcharts.com/highcharts#xAxis.categories) not `axis[0].categories`; and the quotes don't matter – Mottie Jul 07 '14 at 18:08
  • "_it can only work with data in single quotes_" - I don't see how this could be true. Both single and double quotes denote a string literal. `["1.1","2.0"]` and `['1.1','2.0']` should be treated exactly the same. And you can't replace the double quotes with single quotes using javascript because the quotes aren't a part of the _value_ of the string. – Jason P Jul 07 '14 at 18:13
  • yes .. I tested that just Now ..I have Edited and modified my question Now – user3767164 Jul 07 '14 at 18:14

2 Answers2

4

There are a number of problems here:

  1. Your second string literal is broken. Use "'" or '\''.
  2. when used with a string as the first parameter .replace will only replace the first instance found. To replace all instances, use a regular expression with the g flag.
  3. Finally, you're not actually modifying the array in any way because the .replace method returns a new string. Try a simple for-loop instead.

In the end your code should look something like this:

for (var i = 0; i < Arr1.length; i++)
    Arr1[i] = Arr1[i].replace(/"/g,"'");

Given your update, the nature of the question has changed significantly. It now seems that what you want is to simply convert numbers in your array to strings. For that, just use the toString method:

for (var i = 0; i < Arr1.length; i++)
    Arr1[i] = Arr1[i].toString();

Or for brevity, concatenate the value with an empty string:

for (var i = 0; i < Arr1.length; i++)
    Arr1[i] = Arr1[i] + "";

But note, this will remove trailing 0's. A number like 1.0 will be converted to the string like "1". To ensure that the trailing decimals are not trimmed, use toPrecision:

for (var i = 0; i < Arr1.length; i++)
    Arr1[i] = Arr1[i].toPrecision(2);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
2

It seems that you are storing your data as a string in your array.

Your entire objective is basically pointless, since it doesn't matter whether your string literal is built using single quotes '' or double quotes "".

Check out this link for more details on using single vs. double quotes.

Community
  • 1
  • 1
rageandqq
  • 2,221
  • 18
  • 24