-2

I have an array that passed from spring controller to view in thymleaf template.but when I am trying to assign it in a javascript variable it appends double quote at the end of it which I don't want.The assigned array is following-

var array="[1,2,3,5]";

but I want the array will be like this-

var array=[1,2,3,5];
4
  • 1
    is it related to java or javascript ? Commented May 23, 2018 at 18:58
  • 4
    So you ask a question that make no sense and you answer it? Commented May 23, 2018 at 18:59
  • Your question needs work. As it stands, it's really unclear what the issue is. Consider including an minimal reproducible example if possible that outlines your issue. Looking at this question, my immediate thought is, "well, remove the quotes from your code then", and I doubt that's the issue. Commented May 23, 2018 at 18:59
  • If It is about Javascript then use JSON.parse(array). Commented May 23, 2018 at 18:59

2 Answers 2

1

You will need to get the square brackets out of the string and then you can use String.prototype.split() to make it an array, and Array.prototype.map() to make it an array of integers:

let str = "[1,2,3,5]"

str = str.substring(1, str.length-1) //remove first and last char ([ & ])

str = str.split(","); //make an array on every comma

str = str.map(x => parseInt(x)); //parse integers

console.log(str); 

Alternatively you can use JSON.parse() to parse the array:

let str = "[1,2,3,5]";

console.log(JSON.parse(str));

Sign up to request clarification or add additional context in comments.

Comments

0

You just need to parse the string. Your array was JSON.stringify'd. You can also do this with objects

var array="[1,2,3,5]";
array = JSON.parse(array);
console.log(array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.