最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Dart equivalent of Array.prototype.map()? - Stack Overflow

matteradmin5PV0评论

I try to get the id's from List of Maps in Dart. In JavaScript it would be something like this:

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

This should give the result

[3, 4]

Is there a simple way of doing this in Dart?


So far I was able to do this (in Dart):

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

The result is a "MappedListIterable" (not sure what that is) and you cannot use result[0] like you can with a normal List. How can I make a list of this?

I try to get the id's from List of Maps in Dart. In JavaScript it would be something like this:

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

This should give the result

[3, 4]

Is there a simple way of doing this in Dart?


So far I was able to do this (in Dart):

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

The result is a "MappedListIterable" (not sure what that is) and you cannot use result[0] like you can with a normal List. How can I make a list of this?

Share Improve this question edited Apr 27, 2015 at 0:51 Hendrik Jan asked Apr 26, 2015 at 23:47 Hendrik JanHendrik Jan 4,9089 gold badges47 silver badges78 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

See the API for List.map and the API for Iterable (which it returns). You can get the nth element from the iterable using .elementAt(n) or the first element using .first.

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

You can also turn it back into a List using .toList():

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

comment list (0)

  1. No comments so far