如何在node.js中将字符串等数组转换为数组? [英] How to convert an array like string to array in node.js?

查看:736
本文介绍了如何在node.js中将字符串等数组转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我是从node.js中的android设备获取arraylist的.但是因为它是字符串形式,所以我想将其转换为array.为此,我在SO中提到了很多类似的问题,但是它们都没有帮助.我也尝试使用JSON.parse(),但是它没有帮助.

Actually I'm getting the arraylist from android device in node.js . But as it's in string form so I wanna convert it into an array . For that I've referred a lot of similar questions in SO but none of them were helpful . I also tried to use JSON.parse() but it was not helpful.

我正在以'[[艺术,摄影,写作]'"的形式获取SocietyList.那么如何将这种格式转换为数组?

I'm getting societyList in form '[Art, Photography, Writing]'.Thus how to convert this format to an array?

代码:

var soc_arr=JSON.parse(data.societyList)
            console.log(soc_arr.length)

推荐答案

使用类似

var array = arrayList.replace(/^\[|\]$/g, "").split(", ");

更新:

在使用@drinchev建议后使用正则表达式.

UPDATE:

After @drinchev suggestion regex used.

regex匹配char以'['开始,以']'

regex matches char starts with '[' and ends with ']'

这篇关于如何在node.js中将字符串等数组转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆