将花括号中的数组转换为方括号数组 [英] Converting an array in curly braces to a square bracketed array

查看:176
本文介绍了将花括号中的数组转换为方括号数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个数据库,该数据库以以下格式存储字符串数组:

I've inherited a database that stores an array of strings in the following format:

{"First","Second","Third","Fourth"}

这在应用程序中作为有序列表输出.我们目前正在替换前端移动应用(离子/角度),并希望对此数组执行ngFor.在第一个迭代中,我们对花括号进行了快速而肮脏的替换,然后将字符串拆分为,",但希望使用更好的方法.

This is output as an ordered list in the application. We're replacing the front-end mobile app at the moment (ionic / angular) and want to do an ngFor over this array. In the first iteration, we did a quick and dirty replace on the curly brackets and then split the string on "," but would like to use a better method.

将这种类型的字符串视为数组的最佳方法是什么?

What is the best method for treating this type of string as an array?

推荐答案

您可以将花括号替换为方括号:

You could do a string replacement of braces to brackets:

str.replace(/{(.*)}/, '[$1]')

然后可以将此特定字符串解析为数组(通过JSON.parse).

This particular string could then be parsed as an array (via JSON.parse).

这篇关于将花括号中的数组转换为方括号数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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