Flutter: how to return the value by using other value inside the List<Map> - flutter

I have a List<Map<String, String>> like below
[
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
]
And, the ID list **List** as ['bb', 'aa']. By using the ID list, I want to return a new list ['Jane', 'John'] as **List _selectedList**.
I have tried to do it with the .**indexWhere**, however, I am stuck on the List where it has more than one value.
How can I return the List only with the name-value when there is more than one value to look for?

void main() {
var a = [
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
];
var b = ['bb', 'aa'];
var c = a.where((m) => b.contains(m['id'])).map((m) => m['name']);
print(c);
}
Result
(John, Jane)

Use a set to filter out the IDs efficiently.
var ids = ["aa", "cc"];
var idSet = Set<String>.from(ids);
var json = [
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
];
var _selectedList = json.where((data) => idSet.contains(data["id"]))
.map((data) => data["name"]).toList();
print(_selectedList);
Here, .where filters out the data where the ID matches one in the input list "IDs". Sets make the process efficient. Then, the resultant objects are passed to .map where the "name" field is extracted. Finally, the whole thing is converted into a list thus returning the list of names.
Output of the above code:
[John, Lisa]

Related

Flutter - Get or access to matching map(s) from a List of Maps

Here I have a list of maps.
[
{
'name': 'name1',
'age': 30,
},
{
'name': 'name2',
'age': 20,
},
{
'name': 'name1',
'age': 15,
},
]
I need to access to 'name': 'name1' map from above list.
-- Access or extract maps --
i. e. List[0] and(or) List[2]
How do I do this kind of thing???
You can loop through the list and find items, .where will return list of found item. .firstWhere for single item return.
final data = [
{
'name': 'name1',
'age': 30,
},
{
'name': 'name2',
'age': 20,
},
{
'name': 'name1',
'age': 15,
},
];
final findValue = "name1";
final result = data.where((element) => element["name"] == findValue);
print(result); //({name: name1, age: 30}, {name: name1, age: 15})
result.forEach((element) {
print("${element["name"]} ${element["age"]}");
});
More about List

ipycytoscape, draw subgraph

I'm noob in ipycytoscape and Jupyter and seems need help. I'm trying to find a sutable way to show/draw subgraph after I click by node.
For example.
I have graph with node1---node2---node3 . I click by node1 and want to show
node1 --- node2.1 - node2.2 node2.3 --- node3.
Don't know how to make this. Tried to find in Google but coudn't find any suitable approach.
This is my simplest code for this. I no idea how to show and aftger hide (expand and collapse) subgraph. This code don't have this part.
I heard something about layouts but couldn't find sutable example for ipycytoscape.
Any ideas are welcome.
import ipycytoscape
import ipywidgets as widgets
import networkx as nx
counter = 0 # need to make new nodes with uniq id
data = {
'nodes': [
{ 'data': { 'id': 'desktop', 'name': 'Cytoscape', 'href': 'http://cytoscape.org' } },
{ 'data': { 'id': 'a', 'name': 'Grid', 'href': 'http://cytoscape.org', 'parent': 'Cola' } },
{ 'data': { 'id': 'c', 'name': 'Popper', 'href': 'http://cytoscape.org', 'parent': 'Cola' } },
{ 'data': { 'id': 'b', 'name': 'Cola', 'href': 'http://cytoscape.org' } },
{ 'data': { 'id': 'js', 'name': 'Cytoscape.js', 'href': 'http://js.cytoscape.org' } }
],
'edges': [
{'data': { 'source': 'desktop', 'target': 'js' }},
{'data': { 'source': 'a', 'target': 'b' }},
{'data': { 'source': 'a', 'target': 'c' }},
{'data': { 'source': 'b', 'target': 'c' }},
{'data': { 'source': 'js', 'target': 'b' }}
]
}
custom_inherited = ipycytoscape.CytoscapeWidget()
custom_inherited.graph.add_graph_from_json(data)
custom_inherited.set_style([{
'selector': 'node',
'css': {
'content': 'data(name)',
'text-valign': 'center',
'color': 'white',
'text-outline-width': 2,
'text-outline-color': 'green',
'background-color': 'green'
}
},
{
'selector': 'node[classes = "collapsed-childS"]',
'css': {
'background-color': 'red',
'line-color': 'blue',
'target-arrow-color': 'red',
'source-arrow-color': 'red',
'text-outline-color': 'red'
},
}
])
# When I click to node by mouse I add some nodes. I made this because don't know how to show and hide subgraph. The question in this place.
def paint_blue(event):
global counter
for node in cytoscapeobj.graph.nodes:
if node.data['id'] == event['data']['id']:
auxNode = node
auxNode.data['classes'] = "collapsed-childS"
station_NUR = ipycytoscape.Node()
station_NUR.data['id'] = "NUR" + str(counter)
station_NUR.data['name'] = station_NUR.data['id']
station_NUR.data['classes'] = "collapsed-childS"
station_FRA = ipycytoscape.Node()
station_FRA.data['id'] = "FRA" + str(counter)
station_FRA.data['name'] = station_FRA.data['id']
station_FRA.data['classes'] = "collapsed-childS"
new_edge1 = ipycytoscape.Edge()
new_edge1.data['id'] = "line6" + str(counter)
new_edge1.data['source'] = station_NUR.data['id']
new_edge1.data['target'] = station_FRA.data['id']
new_edge2 = ipycytoscape.Edge()
new_edge2.data['id'] = "line7" + str(counter)
new_edge2.data['source'] = station_NUR.data['id']
new_edge2.data['target'] = auxNode.data['id']
custom_inherited.graph.add_node(station_NUR)
custom_inherited.graph.add_node(station_FRA)
custom_inherited.graph.add_edges([new_edge1,new_edge2])
counter = counter + 1
custom_inherited.on('node', 'click', paint_blue)
custom_inherited

Flutter: applying removeAt to a List with the nested objects

I have a List that contains nested objects.
List _haha = [
{id: 'aaa'},
{id: 'bbb'},
{id: 'ccc'},
{id: 'ddd'},
];
I want to delete 'aaa' inside the _haha. Currently, I did
int _indexVal = _haha.indexWhere((e) => e['id'] == 'aaa');
And, use this int value to remove the item inside the list. Is there any way that I can use .removeAt with one single line?
Oneliner with .removeAt():
void main() {
List<Map<String, String>> haha = [
{'id': 'aaa'},
{'id': 'bbb'},
{'id': 'ccc'},
{'id': 'ddd'},
];
haha.removeAt(haha.indexWhere((item) => item['id'] == 'aaa'));
}
But, you may be more interested in using the .removeWhere() method.
void main() {
List<Map<String, String>> haha = [
{'id': 'aaa'},
{'id': 'bbb'},
{'id': 'ccc'},
{'id': 'ddd'},
];
haha.removeWhere((item) => item['id'] == 'aaa');
}

PyMongo: Removing a nested object without knowing the key

Let's say I have a collection that looks like this:
{
'_id': ObjectId('abc'),
'customer': 'bob',
'products': {
'1234':
{'name': 'Shirt',
'productID': 5
},
'5679': {
'name': 'Hat',
'productID': 5
}
}
'1011': {
'name': 'Jeans',
'productID': 9
}
}
}
I am looking to remove all nested objects whose 'productID' property is 5, so the collection would look like this afterwards:
{'_id': ObjectId('abc'),
'name': 'bob',
'products': {
'1011': {
'name': 'Jeans',
'productID': 9
}
}
}
I know the following information:
customer: bob
productID: 5
Is it possible to do a wildcard on 'products'? Something like this (it does not work):
db.update({'customer':'bob'}, {'$unset': {'products.*': {'productID': 9}})
If you have a choice, refactor your data to make each item a list element, e.g.
{
'customer': 'bob',
'products': [
{'code': '1234',
'name': 'Shirt',
'productID': 5
},
{'code': '5679',
'name': 'Hat',
'productID': 5
},
{'code': '1011',
'name': 'Jeans',
'productID': 9
}
]
}
Then your update becomes a piece of cake:
db.mycollection.update_one({'customer': 'bob'}, {'$pull': {'products': {'productID': 5}}})
result:
{
"customer": "bob",
"products": [
{
"code": "1011",
"name": "Jeans",
"productID": 9
}
]
}
Persisting with poor choices of schema will no yield long term rewards.

GroupBy and CountBy for multiple values

I am trying to use underscorejs to groupBy multiple values based on below data.
var obj = [
{
'Name': 'A',
'Status': 'Ticket Closed'
},{
'Name': 'B',
'Status': 'Ticket Open To Close'
},{
'Name': 'A',
'Status': 'Ticket Closed'
},{
'Name': 'B',
'Status': 'Ticket Open'
},{
'Name': 'A',
'Status': 'Ticket Open To Closed'
},{
'Name': 'A',
'Status': 'Ticket Open'
},{
'Name': 'B',
'Status': 'Ticket Open'
}
];
The expected output is
[{
'Name': Closed',
'Count': [2, 0]
},{
'Name': Open',
'Count': [2, 3]
}]
First object counts all closed tickets (Where the status contains the word closed) for A and B simultaneously. Similarly the second object counts for Open tickets. Here is what I tried
var arr = _.groupBy(obj, function (row) {
return (row["Status"].indexOf('Open') > 0 ? "Open" : "Closed");
});
var arr1 = _.groupBy(arr["Closed"], 'Name');
var arr2 = _.groupBy(arr["Open"], 'Name');
var cData = [];
cData.push(arr1);
cData.push(arr2);
Unable to get count from same code. Any help?
Ok. So I was able to resolve this. Here's the working example
http://jsfiddle.net/7ETKV/
Here is the solved code:
var jsondata = jsondata.sort(function (a, b) {
return a.Name.localeCompare(b.Name);
});
_.each(jsondata, function (row, idx) {
((row.Status.indexOf('Open') > 0) ? jsondata[idx].bgStatus = 'Open' : jsondata[idx].bgStatus = 'Closed');
});
var cdata = [], xobj = {};
var gd = _.groupBy(jsondata, 'bgStatus');
var cats = _.keys(_.groupBy(jsondata, 'Name'));
for (var p in gd) {
var arr = _.countBy(gd[p], function (row) {
return row.Name;
});
_.each(cats, function (row) {
if (!arr.hasOwnProperty(row)) arr[row] = 0;
});
xobj.Name = p;
xobj.Data = _.values(arr);
cdata.push(xobj);
xobj = {};
}
Hope might help someone. Any updates to the code / optimizations are welcome.