Dart - How to create a new List<Object2> from an existing List<Object1> - dart

I have an existing List i.e.
[
{
"name": "miller",
"age": 22,
"nationality": "Romania",
"doj": "2018-09-17",
},
{
"name": "sochi",
"age": 35,
"nationality": "Poland",
"doj": "2018-10-11",
},
{
"name": "diana",
"age": 35,
"nationality": "Belarus",
"doj": "2018-12-14",
}
]
and would like to create a new List using the above list (with fewer fields) i.e.
[
{
"name": "miller",
"nationality": "Romania",
},
{
"name": "sochi",
"nationality": "Poland",
},
{
"name": "diana",
"nationality": "Belarus",
}
]

You can map the list and either remove the key that you want to be removed or copy all the keys that you want to keep.
var newList = list.map((e) => e..remove('doj'));
//or
var newList = list.map((e) => {'name': e['name'], 'age': e['age'], ...);

Related

Schema definition in Swagger

I'm very new to OpenAPI and I'm using http://editor.swagger.io to design an API.
I'm stuck in Schema with a JSON looking like following
{
"CORRELATION_ID": "10",
"CONTROL":
{
"DAS_IS_RECIPIENT": "123",
"DOCTPYE": "ert",
"PROCESS_INDICATOR": "nord"
},
"HEADER":
{
"ID": "456",
"INVOICE_NUMBER": "678",
"DMS_DOC_ID": "876",
"INVOICE_DATE": "10082020"
},
"ITEMS": [
{
"SHORT_TEXT": "123",
"LSTAR": 0,
"QUANTITY": "23"
},
{
"SHORT_TEXT": "456",
"LSTAR": 234,
"QUANTITY": "21"
}
],
"DEBITOR":
{
"ID": "444",
"FIRSTNAME": "nick",
"LASTNAME": "cantre"
},
"CREDITOR":
{
"ID": "454",
"FIRSTNAME": "ava",
"LASTNAME": "pierre"
}
}
How to create a schema according to this JSON structure?

Neo4j Cypher : return nodes with id as a dictionary

I have nodes with these properties:
MATCH (n:A) RETURN n
[
{
"name": "114s09A.1",
"_id": "114s09A.1",
"id": "114s09A.1",
"created_n4j": "2020-12-21T09:56:11.256000000Z",
"type": "A",
"updated_n4j": "2020-12-21T09:56:11.256000000Z"
},
{
"name": "114s09A.2",
"_id": "114s09A.2",
"id": "114s09A.2",
"created_n4j": "2020-12-21T09:56:11.257000000Z",
"type": "A",
"updated_n4j": "2020-12-21T09:56:11.257000000Z"
}
]
Is there a way to build the cypher query so that the result would be shaped as a dictionary where id would be the key ?
[
{
"114s09A.1": {
"name": "114s09A.1",
"id": "114s09A.1",
"created_n4j": "2020-12-21T09:56:11.256000000Z",
"type": "A",
"updated_n4j": "2020-12-21T09:56:11.256000000Z"
}
},
{
"114s09A.2": {
"name": "114s09A.2",
"id": "114s09A.2",
"created_n4j": "2020-12-21T09:56:11.257000000Z",
"type": "A",
"updated_n4j": "2020-12-21T09:56:11.257000000Z"
}
}
]
The closest I came up to so far is :
MATCH (n:A) RETURN n._id AS _id, properties(n) AS properties
[
{
"_id":"114s09A.1",
"properties":{
"name": "114s09A.1",
"id": "114s09A.1",
"created_n4j": "2020-12-21T09:56:11.256000000Z",
"type": "A",
"updated_n4j": "2020-12-21T09:56:11.256000000Z"
}
},
{
"_id":"114s09A.2",
"properties":{
"name": "114s09A.2",
"id": "114s09A.2",
"created_n4j": "2020-12-21T09:56:11.257000000Z",
"type": "A",
"updated_n4j": "2020-12-21T09:56:11.257000000Z"
}
}
]
It's not possible with the default Cypher syntax, however if you have the apoc library installed, you can do this :
MATCH (n:A)
RETURN apoc.map.setKey({}, n.id, n{.*})

How to sum items in a List in Dart

What's the best way to sum all the "amounts" of all "expenses", for each user?
I've tried a few different things but I can't quite get it right. It should return 2 values: 20.0 and 24.90
[
{
"id": 3,
"company": {
"id": 2
},
"user": {
"id": 3,
"first_name": "Fred",
"last_name": "Smith",
"email": "asdfasf",
"is_suspended": false,
"vendor_id": "FS-100",
"username": "etytyurtyu",
"expense": [
{
"id": 7,
"date": "2019-12-14T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyAccount",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL8",
"URL9"
],
"user": {
"id": 3
},
"expense_type": {
"id": 4
},
"booking": {
"id": "HI-3565346"
}
}
]
}
},
{
"id": 2,
"company": {
"id": 2
},
"user": {
"id": 2,
"first_name": "Pierre",
"last_name": "XXXMar",
"email": "asdfasdfads",
"is_suspended": false,
"vendor_id": "PM-100",
"username": "asdfas",
"expense": [
{
"id": 2,
"date": "2019-12-16T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 5,
"date": "2019-12-08T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 3,
"date": "2019-12-17T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyCard",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL5",
"URL6"
],
"user": {
"id": 2
},
"expense_type": {
"id": 12
},
"booking": {
"id": "HI-3565346"
}
}
]
}
}
]
Thanks
You would first map the expenses amounts, then fold it into a single value, like this :
double sum = expenses.map((expense) => expense.amount).fold(0, (prev, amount) => prev + amount);
List item
For the sake of completeness: In 2021 you can replace the .fold(0, (prev, amount) => prev + amount) part by just calling .sum on the list. Just import import 'package:collection/collection.dart'; first –
tmaihoff

How to Insert Options with Ajax while Having Option Group in select2?

I have a multiselect dropdown with below data:
var fruitArray = [];
var fruitGreen = [];
var fruitNotGreen = [];
fruitGreen = {
"text": "Green",
"children": [
{ "id": "Watermelon", "text": "Watermelon" },
{ "id": "Lime", "text": "Lime" },
{ "id": "Guava", "text": "Guava" },
{ "id": "Avocado", "text": "Avocado" },
{ "id": "Kiwi", "text": "Kiwi" },
]};
fruitNotGreen = {
"text": "Not Green",
"children": [
{ "id": "Apple", "text": "Apple" },
{ "id": "Orange", "text": "Orange" },
{ "id": "Berries", "text": "Berries" },
{ "id": "Grape", "text": "Grape" },
{ "id": "Pineapple", "text": "Pineapple" },
]};
fruitArray.push(fruitGreen, fruitNotGreen);
$(".select2").select2({
placeholder: "-- Select Fruits --",
allowClear: true,
maximumSelectionLength: 50,
dropdownAutoWidth: true,
data: fruitArray,
multiple: true,
closeOnSelect: false
});
How to Insert Options with Ajax while Having Option Group? Please help
I'm using select2 multiple dropdown from https://select2.org

Swift Alamofire get data to equal

Hi Im get json data with Alamofire and get like this:
{
"prices": [
{
"id": 1,
"value": 1.327,
"stationId": 24,
"type": 0,
"score": 5
},
{
"id": 2,
"value": 1.319,
"stationId": 25,
"type": 0,
"score": 4
},...],
"stations": [
{
"id": 24,
"name": "...",
"address": "...",
"brandId": 1,
"location": ".."
},
{
"id": 25,
"name": "..",
"address": "..",
"brandId": 1,
"location": ".."
},..],
"brands": [
{
"id": 6,
"name": "AGIP"
},
{
"id": 2,
"name": "EKO"
}, ...]
How can I get all data with "type": 0
And then when get all data with type compare id from prices stations and brands and put to array or dictionary
You could map your response using this pod AlamofireObjectMapper and then you can filter this object using for cycle.

Resources