javascript - Dart equivalent of Array.prototype.map()? - Stack Overflow
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?
1 Answer
Reset to default 8See the API for List.map and the API for Iterable (which it returns). You can get the n
th 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();
- 拍照比“剪刀手”注意了!专家:会泄露指纹信息
- 台北电脑展10大杀手级电脑硬件
- 抓住“苹果”的尾巴,国产平板的突围之路
- node.js - Mongoose schema worked with the main db but not with test db - Stack Overflow
- javascript - In Quill, how to add one button to the toolbar to add some text? - Stack Overflow
- c# - Loading data to dataGridView freezes the window during loading, why threading does not work here? - Stack Overflow
- python - NaN values in Pandas are not being filled by the interpolate function when it's applied to a full dataframe - S
- python - Issues with getting Tensorflow to work with RTX 4060 Laptop - Stack Overflow
- swiftui - Swift Mocking a throwing function - Stack Overflow
- database - MongoDB total keys examined when multiple documents have the same key value - Stack Overflow
- python - Azure Cognitive Vector search query and index creation - Stack Overflow
- android - OneSignal Not Subscribing Users - Stack Overflow
- tensorflow - How to resolved the import error with scipy when using keras_tuner? - Stack Overflow
- java - Android physical keyboard support for key press and hold - Stack Overflow
- applescript - How do I interact with the macOS share sheet? - Stack Overflow
- command prompt - byobu - how to disable byobu_prompt_runtime - Stack Overflow
- c++20 - Why can't C++ tell return_void and return_value are mutually exclusive? - Stack Overflow