我有以下列表:
nested_list = [
["[63]"],
["[61]"],
["[7]"],
["[63]"],
["[80, 18]"],
["[80, 43, 18, 20]"]
]
我知道这是一团糟,但这是我现在必须处理的数据 我需要它是这样的:
[63, 61, 7, 63, 80, 18, 80, 43, 18, 20]
所以基本上将其转换为数字列表。使用尽可能少的循环是否可行?
我使用了 flat() nested_list .flat(2)
并得到了以下结果:
[ '[63]', '[61]', '[7]', '[63]', '[80, 18]', '[80, 43, 18, 20]' ]
还尝试在 this 之后进行 reduce + concat但因为它是一个字符串,所以它不起作用,我不确定从这里到哪里去。
最佳答案
.flatMap()
+ JSON.parse()
可以很容易地做到这一点
const nested_list = [
["[63]"],
["[61]"],
["[7]"],
["[63]"],
["[80, 18]"],
["[80, 43, 18, 20]"]
];
const result = nested_list.flatMap(JSON.parse);
console.log(result)
关于javascript - 将存储为 String 的 Array 转换为 Java String 中的 List 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69300829/