簡體   English   中英

Dart相當於Array.prototype.map()?

[英]Dart equivalent of Array.prototype.map()?

我嘗試從Dart中的地圖列表中獲取id。 在JavaScript中它將是這樣的:

var list = [{id:3, name:'third'}, {id:4, name:'fourth'}];
var result = list.map(function(x){return x.id;});

這應該給出結果

[3, 4]

在Dart有一個簡單的方法嗎?


到目前為止,我能夠做到這一點( 在Dart中 ):

var list = [{'id':3, 'name':'third'},{'id':4, 'name':'fourth'}];
var result = list.map((x) => x['id']);

結果是“MappedListIterable”(不確定是什么),你不能像普通的List一樣使用result[0] 我怎樣才能列出這個?

請參閱List.mapAPI和IterableAPI (它返回)。 你可以得到n從迭代使用th元素.elementAt(n)使用或第一要素.first

var list = [{'id':3, 'name':'third'},{'id':4, 'name':'fourth'}];
var result = list.map((x) => x['id']).first;

您還可以使用.toList()將其重新轉換為List

var resultList = list.map((x) => x['id']).toList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM