1

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.

String Array

When I print out the regular string, like so-

console.log(x);

This is what shows up-

Array

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)?

3
  • 1
    Why are you using eval? 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. 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 that eval doesn't have to do with your specific question, but I'm thinking it's a possible XY problem. Commented Feb 19, 2016 at 3:23

4 Answers 4

3

Use 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]]
Sign up to request clarification or add additional context in comments.

Comments

2

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]]

Comments

2

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;

Comments

0

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 complete reference.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.