-3

This is a basic question in JavaScript. I tried a lot playing around this, but i can't the solution.

I have an array.

var keys = ["a", "b", "c"];
var array =[];

I am looping this keys to form another array

for (var i =0; i<keys.length; i++){
array[i]= "x" + keys[i];
}

If i print this array

i got ['xa']; but what i needed is ["xa"] only difference is the quotes ""..

Why because i have to use this array in another json as with double quotes. Thanks in advance..

consider my json array name as final,

var query = {
            "_source": {
                "include": final
              }.

NOw here the query is forming like

var query = {
            "_source": {
                "include": '["xa"]'
              }

But i need:

var query = {
            "_source": {
                "include": ["xa"]
              }
Subburaj
  • 5,114
  • 10
  • 44
  • 87
  • Your browser will likely be outputting it as `'`. Others will output it as `"`. You need to check and replace youself. – ArtOfCode Nov 05 '14 at 17:53
  • 1
    Are you by any chance looking for `JSON.stringify()`? – The Paramagnetic Croissant Nov 05 '14 at 17:55
  • @The Paramagnetic Croissant thanks.. But when i use that array in my json its coming as '["xa"]' – Subburaj Nov 05 '14 at 18:10
  • @Subburaj that's because the browser "helpfully" surrounds strings with quotes so that you know it's a string. The resulting string ***DOES NOT*** contain the extraneous quotes. – The Paramagnetic Croissant Nov 05 '14 at 18:13
  • @ The Paramagnetic Croissant i am forming an query in my node.js app, so that query will accept the query in ["xa"] this format only.. – Subburaj Nov 05 '14 at 18:14
  • 1
    @Subburaj Since you seem to be having trouble understanding what we are telling you about the quotes around strings, please provide us with the specifics (including code) of what you are trying to do, and perhaps we can show you by example. – forgivenson Nov 05 '14 at 18:37

3 Answers3

2

I think you are confused about what you are seeing printed in your console. Strings don't inherently have either single or double quotes associated with them. So, it doesn't matter if your browser displays it as ['a','b','c'] or ["a","b","c"]. The array still contains the same thing. What you DO care about is how it is converted into a string, as you stated in your question. You said you need it to use double quotes. Try using JSON.stringify(). You will notice that this outputs a string in the format you want. Here is a short example:

var myArray = ['a', 'b', "c"];
// notice I even used a mix of single and double quotes in the declaration
var arrayAsString = JSON.stringify(myArray);
console.log(arrayAsString);

The output will be:

["a","b","c"]
forgivenson
  • 4,394
  • 2
  • 19
  • 28
  • thanks, i got the array resut as ["xa"], as i already said i am using this array in an json, when i use this array in json i am getting '["xa"]' what can i do for that.. – Subburaj Nov 05 '14 at 18:17
  • What do you mean by "using this array in an json"? It is unclear what you are trying to do. – forgivenson Nov 05 '14 at 18:19
1

If you can pass just through via json it mustn't be problem but if you first read it as a string and after you use that string as an argument, you can use replace function. You can do it as I gave here. How to replace all occurrences of a string in JavaScript?

Community
  • 1
  • 1
zorkaya
  • 113
  • 1
  • 9
  • I dont want it to convert into string, because when i use it in json its coming with singelquotes.. – Subburaj Nov 05 '14 at 17:57
0

Use single quotes and put double quotes inside of it. array[i]= '"x' + keys[i] + '"';

jsdev
  • 26
  • 3