gpt4 book ai didi

json - Google Dart删除DropDown重复项

转载 作者:行者123 更新时间:2023-12-03 03:46:13 25 4
gpt4 key购买 nike

我正在尝试从 map 中删除重复项以填充下拉列表。但是,我无法成功将其删除。下拉列表填充的数据量(JSON)是实际数据量的两倍。如果有简单的方法可以做到这一点,请告诉我。另外,如果您需要更多信息,请告诉我。

Dart 代码:

var jsonString = response;
//var dropDownValue = shroot.querySelector("#asset");
SelectElement dropDown = shroot.querySelector("#asset");

Map jsonObject = JSON.decode(jsonString) as Map;
dropDownList = jsonObject["eee"] as List<Map>;

LinkedHashMap<String, Map> dataMap = new LinkedHashMap<String, Map>();

for(Map d in dropDownList)
{
dropDown.children.add(new OptionElement(data: d['displayName'], value: d['displayName']));

print(d);
print(d["id"]);
print(d["displayName"]);
}

Dart 尝试2:
var ddValues = dropDownList
// extract the 'displayValue'
.map((e) => e['displayName'])
// create a set to eliminate duplicates
.toSet().toList()
// sort the result
// sort changes the list it is called - it doesn't return a new list
// therefore we have to chain it using `..` instead of `.`
..sort();

ddValues.forEach((e) {
print(e);
dropDown.children.add(new OptionElement(data: e, value: e));
});

最佳答案

尚不清楚您的数据看起来如何,但我想您想要类似

void main() {

// to make the code here shorter I remove the elements from JSON that don't have any effect on the processing. This code should work the same with the full JSON posted in your question.
var values = {
"serviceResponseValue":[
{"displayName":"name"},
{"displayName":"name"},
{"displayName":"name1"},
{"displayName":"Processes"}
],
"messages":{"messages":[]}
};


var ddValues = values['serviceResponseValue']
// extract the 'displayValue'
.map((e) => e['displayName'])
// create a set to eliminate duplicates
.toSet().toList()
// sort the result
// sort changes the list it is called - it doesn't return a new list
// therefore we have to chain it using `..` instead of `.`
..sort();

ddValues.forEach((e) {
print(e);
//dropDown.children.add(new OptionElement(data: e, value: e));
});
}

关于json - Google Dart删除DropDown重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25247138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com