gpt4 book ai didi

javascript - 将字符串加载到数组的数组中

转载 作者:行者123 更新时间:2023-11-28 12:13:44 24 4
gpt4 key购买 nike

我得到一组字符串格式的数组,看起来像

[49,16,135],[51,16,140],[50,18,150]

现在我需要将它们保存在数组的数组中。我试过了

let array = [];
let str = '[49,16,135],[51,16,140],[50,18,150]';
array = str.split('[]');

console.log(array);

但它只创建一个数组,包括所有字符串作为一个元素,而我需要拥有

array = [[49,16,135],[51,16,140],[50,18,150]]

最佳答案

在字符串的每一端添加数组分隔符,然后使用 JSON.parse:

const str = '[49,16,135],[51,16,140],[50,18,150]';
const json = '[' + str + ']';
const array = JSON.parse(json);
console.log(array);

关于javascript - 将字符串加载到数组的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54603784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com