简体   繁体   English

提取一个字符串数组,它是 javascript 中的一个字符串

[英]Extracting an array of strings which is a string in javascript

I have a string stored in a variable我有一个字符串存储在一个变量中

clickedArray = '["0", "1", "2", "3"]' clickedArray = '["0", "1", "2", "3"]'

This array is long and i have to extract the array inside this string into这个数组很长,我必须将这个字符串中的数组提取到

array = ["0", "1", "2", "3"]数组 = ["0", "1", "2", "3"]

i then have to match every element of this array to a number which i will manage on my own.然后我必须将这个数组的每个元素与我自己管理的数字相匹配。 I am however struggling to extract this array from a string然而,我正在努力从字符串中提取这个数组

I tried splitting the string but it yielded weird results and did not get what i expected.我尝试拆分字符串,但结果很奇怪,没有达到我的预期。

Parse the string as JSON:将字符串解析为 JSON:

 const clickedArray = '["0", "1", "2", "3"]' const array = JSON.parse(clickedArray) console.log(array)

You can use the parse function on JSON lib string and convert it to array您可以在JSON lib 字符串上使用解析 function 并将其转换为数组

clickedArray = '["0", "1", "2", "3"]';
let array = JSON.parse(clickedArray);
console.log(array); // ["0", "1", "2", "3"]

docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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