Usr*_*Usr 0 javascript arrays string split
我有这个字符串(称为 currentExecution.variables):
{executionid=0c3246fb37e65e3368c8c4f30000016ab593bec244daa8df, timeout=10000}
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为地图,以便我可以处理条目,但我很难做到这一点。我尝试按照此答案将其转换为键值对集。首先,我将 = 替换为 : 并将 { 或 } 替换为空格,然后根据答案将其拆分:
newString.split(/,(?=[^,]+:)/).map(s => s.split(': '));
Run Code Online (Sandbox Code Playgroud)
但我没有得到正确的结果,而且我在没有地图的情况下陷入困境。少了什么东西?或者有更好/更快的方法来做到这一点?
您可以执行以下操作
{
和字符。内部有异物时}
请勿使用。replace
let string = "{executionid=0c3246fb37e65e3368c8c4f30000016ab593bec244daa8df, timeout=10000}";
let keyValuePairs = string.slice(1, -1) //remove first and last character
.split(/\s*,\s*/) //split with optional spaces around the comma
.map(chunk => chunk.split("=")); //split key=value
const map = new Map(keyValuePairs);
console.log(map.get("executionid"));
console.log(map.get("timeout"));
Run Code Online (Sandbox Code Playgroud)