I am trying to print out this code in a string.
var x = [[1, 2, 3, 4.5, 6], [7, 8.5, 9.5, 10]];`
console.log(x.toString());
This is what shows up.
When I print out the regular string, like so-
console.log(x);
This is what shows up-
Is there any way I can print out a string with the square brackets? I want the result to be something like "[[1, 2, 3, 4.5, 6], [7, 8.5, 9.5, 10]]"
.
I need to add this value to a eval
function, which is why I need to add the entire array (as a string with the brackets) to the function.
Is there any way this can be possible (preferably without additional libraries)?
I am trying to print out this code in a string.
var x = [[1, 2, 3, 4.5, 6], [7, 8.5, 9.5, 10]];`
console.log(x.toString());
This is what shows up.
When I print out the regular string, like so-
console.log(x);
This is what shows up-
Is there any way I can print out a string with the square brackets? I want the result to be something like "[[1, 2, 3, 4.5, 6], [7, 8.5, 9.5, 10]]"
.
I need to add this value to a eval
function, which is why I need to add the entire array (as a string with the brackets) to the function.
Is there any way this can be possible (preferably without additional libraries)?
Share Improve this question edited Feb 19, 2016 at 3:10 Tushar 87.3k21 gold badges163 silver badges181 bronze badges asked Feb 19, 2016 at 3:01 RonRon 571 silver badge8 bronze badges 3-
1
Why are you using
eval
? – Explosion Pills Commented Feb 19, 2016 at 3:02 - @ExplosionPills the eval function doesn't have to do with the question- but I said that anyway to state why exactly I needed to have the array in a string. I am using eval to assign this value to a variable, but instead the first image shown above is being assigned to the variable, which is causing many errors. This is why I am using eval. – Ron Commented Feb 19, 2016 at 3:13
-
1
What do you need
eval
to assign it to a variable instead of just assigning the array to the variable? I know thateval
doesn't have to do with your specific question, but I'm thinking it's a possible XY problem. – Explosion Pills Commented Feb 19, 2016 at 3:23
4 Answers
Reset to default 3Use JSON.stringify
to convert the Array to a JSON Array.
var x = [[1, 2, 3, 4.5, 6], [7, 8.5, 9.5, 10]];
console.log(JSON.stringify(x));
// [[1,2,3,4.5,6],[7,8.5,9.5,10]]
You can use JSON.stringify()
to serialize x into a string.
console.log(JSON.stringify(x))
will return what you're looking for.
console.log(JSON.stringify(x))
[[1,2,3,4.5,6],[7,8.5,9.5,10]]
You can use Array#map
with string concatenation.
'[' + x.map(e => '[' + e + ']') + ']'
var x = [
[1, 2, 3, 4.5, 6],
[7, 8.5, 9.5, 10]
];
var string = '[' + x.map(e => '[' + e + ']') + ']';
console.log(string);
document.body.innerHTML = string;
If the elements of the main array are not always array, Array.isArray
can be used with ternary operator.
'[' + x.map(e => Array.isArray(e) ? '[' + e + ']' : e) + ']'
var x = [
[1, 2, 3, 4.5, 6],
[7, 8.5, 9.5, 10],
'name',
10
];
var string = '[' + x.map(e => Array.isArray(e) ? '[' + e + ']' : e) + ']';
console.log(string);
document.body.innerHTML = string;
The best way is to use the json.stringify() method.The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified. MAy the below url help you where you can get plete reference.
https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify