Arrays 颤振-阵列中具有相同名称的滤芯
我有一个包含多个元素的数组,其中一些元素具有相同的名称并通过数组重复。我需要得到这个数组的所有元素,只得到一个重复元素和所有非重复元素 我读过dart中的阵列,但没有多大帮助。我没有示例代码,我不知道我是如何开始的 有人知道我怎么做吗 编辑:数组示例 JSON中的许多错误,算了吧,就是一个简单的例子Arrays 颤振-阵列中具有相同名称的滤芯,arrays,flutter,Arrays,Flutter,我有一个包含多个元素的数组,其中一些元素具有相同的名称并通过数组重复。我需要得到这个数组的所有元素,只得到一个重复元素和所有非重复元素 我读过dart中的阵列,但没有多大帮助。我没有示例代码,我不知道我是如何开始的 有人知道我怎么做吗 编辑:数组示例 JSON中的许多错误,算了吧,就是一个简单的例子 [{'id': '19', fabricante: 'AA', modelo: 'h4000 DN100', 'configuracionRaw': 'CC', 'configuracion': {
[{'id': '19', fabricante: 'AA', modelo: 'h4000 DN100', 'configuracionRaw': 'CC', 'configuracion': {'emisor': '4', 'pw': '10.0', 'valorMomento': 'null', 'valorMaximo': '9999999.0', 'dataloggerPeriodoMin': '60', 'dataloggerHora': '08:00','fw': 'null','dataloggerPeriodoFTP': '1', 'dataloggerHoraFTP': '10', 'contadorParado': '240', 'alarmaPeriodo':'3', 'alarmaUmbral': '1000', 'alarmaEspontaneo': 'true'}, {'id': '20', fabricante: 'AA', modelo: 'h4000 DN100', 'configuracionRaw': 'CC', 'configuracion': {'emisor': '4', 'pw': '10.0', 'valorMomento': 'null', 'valorMaximo': '9999999.0', 'dataloggerPeriodoMin': '60', 'dataloggerHora': '08:00','fw': 'null','dataloggerPeriodoFTP': '1', 'dataloggerHoraFTP': '10', 'contadorParado': '240', 'alarmaPeriodo':'3', 'alarmaUmbral': '1000', 'alarmaEspontaneo': 'true'}} ]
代码示例
DataMaker是一个数据数组
dataMaker.forEach((val) {
print(val['fabricante']); //RETURN NAME
if(filteredList!=val['fabricante']()){ //ERROR
filteredList.add(val);
}
});
你可以通过Set完成
为什么不用电视机呢
这将打印以下内容
20
5
60
70
如果您只想获得唯一元素的数组,可以使用“set”或“foreach” 工作省道 方法1
void main() {
List list = [1,1,2,3,3,4,5,5,5,6,6];
print(list.toSet().toList());
}
// answer
// [1, 2, 3, 4, 5, 6]
方法2
void main() {
List list = [1,1,2,3,3,4,5,5,5,6,6];
List filteredList = [];
list.forEach((val) {
if(filteredList.indexOf(val) == -1){
filteredList.add(val);
}
});
print(filteredList);
}
// answer
// [1, 2, 3, 4, 5, 6]
这可能看起来有点原始,但工作正常
List list = [2, 5, 7, 9, 22, 2, 7, 5, 9, 22, 6, 4, 7, 9, 2];
List nonRepetitive = [];
for (var i = 0; i < list.length; i++) {
bool repeated = false;
for (var j = 0; j < nonRepetitive.length; j++) {
if (list[i] == nonRepetitive[j]) {
repeated = true;
}
}
if (!repeated) {
nonRepetitive.add(list[i]);
}
}
print(nonRepetitive);
您可以通过多种方式来实现这一点,其中一种可以使用Array.fold,例如 另一个更详细的解决方案是使用类,因为您首先需要使用类将JSON转换为常规类,例如,并重写==double equal运算符。e、 g 第二个选项更详细,但我认为从长远来看,它更易于维护。
希望能有所帮助。霍萨尔,谢谢你为我指明了正确的方向 我不需要@json_serializable来支持设置,尽管我必须添加@override装饰器/注释
class Person {
String name;
int age;
Person({this.name, this.age});
@override
String toString() {
return '$name $age';
}
@override
bool operator ==(o) => o is Person && name == o.name && age == o.age;
@override
int get hashCode => name.hashCode + age.hashCode; //use a more reliable hashCode
}
你能分享你的代码吗?解决什么样的元素的问题会更有帮助?只是线?物体?至少提供一个数组的示例。此解决方案不适用于自定义对象。在自定义对象上,必须重写==&hasCode。
List list = [2, 5, 7, 9, 22, 2, 7, 5, 9, 22, 6, 4, 7, 9, 2];
List nonRepetitive = [];
for (var i = 0; i < list.length; i++) {
bool repeated = false;
for (var j = 0; j < nonRepetitive.length; j++) {
if (list[i] == nonRepetitive[j]) {
repeated = true;
}
}
if (!repeated) {
nonRepetitive.add(list[i]);
}
}
print(nonRepetitive);
[2, 5, 7, 9, 22, 6, 4]
var withNoDuplications = yourArray.fold([], (current, next) {
var elementExist = current.firstWhere((element) {
return element['id'] == next['id'];
}, orElse: () => null);
if(elementExist == null) {
current.add(next);
}
return current;
});
print(withNoDuplications);
class Person {
String name;
int age;
Person({this.name, this.age});
@override
String toString() {
return '$name $age';
}
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
bool operator ==(o) => o is Person && name == o.name && age == o.age;
int get hashCode => name.hashCode + age.hashCode; //use a more reliable hashCode
}
List list = [Person(), Person(), Person()];
var result = list.toSet().toList()
class Person {
String name;
int age;
Person({this.name, this.age});
@override
String toString() {
return '$name $age';
}
@override
bool operator ==(o) => o is Person && name == o.name && age == o.age;
@override
int get hashCode => name.hashCode + age.hashCode; //use a more reliable hashCode
}