如何在飞镖中弄平一个嵌套的阵列?
我如何弄平一个嵌套的阵列?
[
{
"page": 1,
"items": [
{
"addresses": [
"hello1",
"hello2"
]
}
]
},
{
"page": 2,
"items": [
3,
{
"addresses": [
"hello3",
"hello4"
]
}
]
},
{
"page": 3,
"items": [
3,
4
]
}
];
所需的输出IST:
["hello1", "hello2", "hello3", "hello4"]
import 'dart:convert';
main() {
const jsonString =
'[{"page":1, "items": [{"addresses": ["hello1","hello2"]}]}, {"page":2, "items": [3, {"addresses": ["hello3","hello4"]}]}, {"page":3, "items": [3, 4]}]';
final items = jsonDecode(jsonString) as List;
final x = items.expand((p) => p["items"]).expand((p) => p["addresses"]);
print(x);
}
How do I flatten a nested array?
[
{
"page": 1,
"items": [
{
"addresses": [
"hello1",
"hello2"
]
}
]
},
{
"page": 2,
"items": [
3,
{
"addresses": [
"hello3",
"hello4"
]
}
]
},
{
"page": 3,
"items": [
3,
4
]
}
];
Desired output ist:
["hello1", "hello2", "hello3", "hello4"]
import 'dart:convert';
main() {
const jsonString =
'[{"page":1, "items": [{"addresses": ["hello1","hello2"]}]}, {"page":2, "items": [3, {"addresses": ["hello3","hello4"]}]}, {"page":3, "items": [3, 4]}]';
final items = jsonDecode(jsonString) as List;
final x = items.expand((p) => p["items"]).expand((p) => p["addresses"]);
print(x);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以您有一个看起来像这样的地图列表:
有时这些项目只是数字,您想在单个列表上获取所有地址,这就是我的方式:
有点笨拙,我知道,但是我认为这会解决这个问题
Ok, so you have a list of maps that look like this:
except sometimes the items are just numbers, you want to get all addresses on a single list, here is how I would do it:
A bit clunky, I know, but I think it will do the trick