JavaScript拆分无法按预期工作
我的前端代码:JavaScript拆分无法按预期工作,javascript,Javascript,我的前端代码: const myArray = JSON.stringify( this.someObj.map(a => a.role) ); Data Structure for myArray becomes is ["A", "B"], this got converted to a string. 我的后端代码: response.input.text).split(','); 但是当我拆分它时,我得到了以下数据 [0] = ["A" [1] = "B"] 我怎样才能
const myArray = JSON.stringify(
this.someObj.map(a => a.role)
);
Data Structure for myArray becomes is ["A", "B"], this got converted to a string.
我的后端代码:
response.input.text).split(',');
但是当我拆分它时,我得到了以下数据
[0] = ["A"
[1] = "B"]
我怎样才能优雅地清洁它?所以我只得到A和B,而不使用拆分解析JSON来获得原始数组元素
const someObj=[{role:A},{role:B}];
const myArray=JSON.stringify
someObj.mapa=>a.role
;
result=JSON.parsemyArray;
console.logresult[0];
控制台。日志结果[1];你能发布你正在使用的确切代码吗,这样我们就可以重现你的问题;例如,在您的问题中,您没有演示如何使用拆分。这是在全局上下文中,除非您提供代码。JSON.stringify正在将数组转换为字符串。您的后端是否有任何可以使用的JSON解析方法?使用slice拆分数组只需解析数组const arr=JSON.parsetext并使用arr[0]和arr[1]?正如他所说,不要使用拆分。JSON.parseresponse.input.text应该已经是字符串数组了;stringify转换JSON格式的变量并返回字符串。