简体   繁体   中英

How to convert a String to int array

I have a spring rest controller that returns an int array. But it is received as a String on the front-end.

How can I convert this String [1,1] to int array in Javascript.

edit: turns out, it was JSON.

If the string = [1,1] , then you can simply parse it as json. Like this:

 const str = '[1,1]'; const resultArr = JSON.parse(str); console.log(resultArr); 

The result is an array of integers:

Array:
0: 1
1: 1

This is an example:

var arr = "[1, 1]".replace(/[\[\]]/g, "").split(",");
console.log(arr);

you can replace "[1, 1]" with you variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM