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"]
}
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"]
}
Share
Improve this question
edited Nov 6, 2014 at 5:02
Subburaj
asked Nov 5, 2014 at 17:51
SubburajSubburaj
5,20211 gold badges47 silver badges95 bronze badges
6
-
Your browser will likely be outputting it as
'
. Others will output it as"
. You need to check and replace youself. – ArtOfCode Commented Nov 5, 2014 at 17:53 -
1
Are you by any chance looking for
JSON.stringify()
? – The Paramagnetic Croissant Commented Nov 5, 2014 at 17:55 - @The Paramagnetic Croissant thanks.. But when i use that array in my json its ing as '["xa"]' – Subburaj Commented Nov 5, 2014 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 Commented Nov 5, 2014 at 18:13
- 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 Commented Nov 5, 2014 at 18:37
3 Answers
Reset to default 2I 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"]
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?
Use single quotes and put double quotes inside of it. array[i]= '"x' + keys[i] + '"';