如何将逗号和分号分隔的字符串拆分为二维数组 [英] How to split comma and semicolon separated string into a two dimensional array

查看:496
本文介绍了如何将逗号和分号分隔的字符串拆分为二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个变量 users ,其中包含以下文本字符串,其中每个用户以分号分隔,每个用户的每个属性用逗号分隔:

Let's say I have a variable users that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma:

Bob,1234,Bob@example.com;Mark,5678,Mark@example.com

我如何将其拆分为多维数组变量 userArray ,如下所示: :

How would I split this into a multidimensional array variable userArray that looks like this:

[
    [Bob,1234,Bob@example.com],
    [Mark,5678,Mark@example.com]
]

使用JavaScript?

using JavaScript?

推荐答案

您可以使用 split() map()

You can do this with split() and map()

var str = 'Bob,1234,Bob@example.com;Mark,5678,Mark@example.com';
var result = str.split(';').map(e => e.split(','));
console.log(result)

这篇关于如何将逗号和分号分隔的字符串拆分为二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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