从对象列表中筛选出ID。

huangapple go评论65阅读模式
英文:

Filter ids from an object list

问题

我需要从对象列表中筛选出ID,并将这些ID存储在一个新数组中。请建议一种方法来完成这个任务。根据以下示例,我需要从“_studentDetails”列表中筛选出ID,并将这些ID存储在“_idList”数组中。

示例:

List<int> _idList = [];

List<Student> _studentDetails = [];

_studentDetails = [
       Student(id: 1, name: 'Peter', age: 14),
       Student(id: 2, name: 'Anne', age: 20),
];
英文:

I need to filter ids from an object list and add those id list to a new array.Please suggest a way to do this.As per the below example I need to filter the ids from the '_studentDetails' list and store those ids in '_idList' array.

Example:


List&lt;int&gt; _idList = [];

List&lt;Student&gt; _studentDetails = [];

_studentDetails = [
       Student(id:1 , name:&#39;Peter&#39; , age: 14),
       Student(id:2 , name:&#39;Anne&#39; , age: 20),
];

答案1

得分: 1

你可以使用 list.map() 方法来实现这个,遍历列表项,对于每个项,返回其属性 id 的值,然后将所有返回的值转换为列表:

List<int> _idList = _studentDetails.map((student) => student.id).toList();
英文:

You can achieve this using list.map() method<br>
you loop through the list items and for every one you return the value of its preoperty id then convert all the returned values to list:

List&lt;int&gt; _idList = _studentDetails.map((student) =&gt; student.id).toList();

答案2

得分: 0

如果你只想要ID列表,你可以使用.map方法:

List<int> _idList = [];

List<Student> _studentDetails = [];

_studentDetails = [
       Student(id:1 , name:'Peter' , age: 14),
       Student(id:2 , name:'Anne' , age: 20),
];

_idList.addAll(
  _studentDetails.map((student) => student.id),
);
英文:

If you just want the list of ids, you can use the method .map:

List&lt;int&gt; _idList = [];

List&lt;Student&gt; _studentDetails = [];

_studentDetails = [
       Student(id:1 , name:&#39;Peter&#39; , age: 14),
       Student(id:2 , name:&#39;Anne&#39; , age: 20),
];

_idList.addAll(
  _studentDetails.map((student) =&gt; student.id),
);

答案3

得分: 0

你还可以使用 forEach() 循环 来实现这一点。

下面是代码:

void main(){
  List<int> _idList = [];

  List<Student> _studentDetails = [];

  _studentDetails = [
    Student(id: 1, name: 'Peter', age: 14),
    Student(id: 2, name: 'Anne', age: 20),
  ];

  _studentDetails.forEach((element) {
    _idList.add(element.id);

  });

  print(_idList);
}

class Student{

  int id;
  String? name;
  int? age;

  Student({required this.id, required this.name, required this age});
}
英文:

You can also achieve this using forEach() loop

Below the code:

void main(){
  List&lt;int&gt; _idList = [];

List&lt;Student&gt; _studentDetails = [];

_studentDetails = [
       Student(id:1 , name:&#39;Peter&#39; , age: 14),
       Student(id:2 , name:&#39;Anne&#39; , age: 20),
];
  
  _studentDetails.forEach((element) {
    _idList.add(element.id);
    
  } );
  
  print(_idList);
}

class Student{

  int id;
 String? name;
  int? age;
  
  Student({required this.id, required this.name, required this.age});
}

huangapple
  • 本文由 发表于 2023年3月9日 13:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定