Array that store color maps flutter - android-studio

¿How can we create an array to store colors in dart, with this structure style?
"1":{
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
},
"2":{
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
},
I created a map trying to achieve that
Map<String, Color> btncolor = {
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
};
but it needs 3 arguments to have an index, and its not possible to do it.
Map<String, String, Color> btncolor = {
"1":{
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
},
"2":{
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
},
};
As im kinda new to Flutter coding any hint or help would be appreciated, ty.

To create a List of 20 elements with the same element,(here the element is Map Object that maps String to Color), Try this,
Map<String,Color> btnColorMap = {
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
}
var list = List<Map>.filled(20, btnColorMap);

Just do that, change Map<String, String, Color> to Map<String, Map<String, Color>> as if you are telling dart that you want a map of maps. because the map constructor only takes 2 arguments.
so the final result should be like this:
Map<String, Map<String, Color>> btncolor = {
"1":{
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
},
"2":{
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
},
};

Related

how use _.mergewith of lodash and customizer function

Hi i need merge two object with mergewith in lodash ...
object1 = {
"a": {
"name": "masoud", "age": 3, "user": "masmas"
},
"b" : {
"name": "milad", "age": 13, "user": "milmil"
}
}
object2 = {
"b" : {
"pythonSkill" : 18 , "HTMLSkill" : 15
} ,
"c" : {
"phpSkill" : 15 , "CSSSkill" : 16
},
"a" : {
"javaSkill" : 20 , "reactSkil" : 10
} ,
}
and finally combined object sample :
final = {
"a": {
"name": "masoud", "age": 3, "user": "masmas", "persianLanguage": 20, "englishLanguage": 10
},
"b" : {
"name": "milad", "age": 13, "user": "milmil" , "pythonSkill": 18, "HTMLSkill": 15
}
}
Each object does not have a specific number of internal objects and may be different each time .
one time Object1 have a , b , c , .... , m & other time Object1 have m , n , o , c , ...
Object2 similar object1 ...
Different scenarios
1- if Object1 have "a" and object2 have "a" , finally object must have "a" ..
2- if Object1 have not "a" and object2 have "a" , finally object have not "a" ..
3- if Object1 have "a" and object2 have not "a" , finally object have not "a"
I don't know about using _.mergeWith, this seems to me to be rather difficult but you can use _.mapValues and _.pickBy to achieve the desired result relatively easily
object1 = {
"a": {
"name": "masoud", "age": 3, "user": "masmas"
},
"b" : {
"name": "milad", "age": 13, "user": "milmil"
},
"d" : {
"name": "nassim", "age": 32, "user": "nm1"
}
}
object2 = {
"b" : {
"pythonSkill" : 18 , "HTMLSkill" : 15
} ,
"c" : {
"phpSkill" : 15 , "CSSSkill" : 16
},
"a" : {
"javaSkill" : 20 , "reactSkil" : 10
} ,
}
let final = _.mapValues(_.pickBy(object1, (v,k) => object2[k]), (v, k, o) => {
return { ...v, ...object2[k] }
});
console.log(final)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

filtering groovy Map and remove redundant keys

I have a Groovy map Map<String, List<String>>. I need to go through each String list, remove those meet a condition (for example length > 3). If all elements removed from the list, remove the key from the map.
For example
{
"1" : ["1", "22,", "333", "4444"],
"2" : ["2", "2222"],
"4" : ["444444"]
}
becomes
{
"1" : ["1", "22,", "333"],
"2" : ["2"],
}
If there's any easy inplace method can do it.
Try this:
m = [
"1" : ["1", "22,", "333", "4444"],
"2" : ["2", "2222"],
"4" : ["444444"]
]
m.each {String k, List v ->
v.removeAll { it.length() > 3}
if (m[k].isEmpty()) {
m.remove(k)
}
}
m.each { k, v -> println "${k} : ${v}"}
An alternative to #ou_ryperd's answer that doesn't mutate the original map is:
def m = [
"1" : ["1", "22", "333", "4444"],
"2" : ["2", "2222"],
"4" : ["444444"]
]
def newMap = m.collectEntries { k, v -> [k, v.findAll { it.length() < 4 }] }
.findAll { k, v -> v.size() > 0 }

Aerospike - add new key/value pair in nested object

In Aerospike, how can I add a new key/value pair in nested object stored in bins of type map?
For ex,
I have a bins of type map against which I need to store below key/value pairs.
{
"a" : "apple",
"b" : "ball",
"c" : { "d" : "dog", "e" : "elephant" },
"f" : { "g" : { "h" : "horse" } },
"i" : { "j" : "jackal", "k" : { "l" : "lion", "m" : "monkey" } }
}
Now, I want to update an existing nested object against key "k" to add one more key value pair like below.
"k" : { "l" : "lion", "m" : "monkey", "n" : "nest" }
Final result should be like below.
{
"a" : "apple",
"b" : "ball",
"c" : { "d" : "dog", "e" : "elephant" },
"f" : { "g" : { "h" : "horse" } },
"i" : { "j" : "jackal", "k" : { "l" : "lion", "m" : "monkey", "n" : "nest" } }
}
Any suggestions on how to achieve this?
It's a NodeJS (10.6.0) application & I'm using NodeJS aerospike client (3.6.1) to interact with Aerospike (4.3.0.7).
Updates on nested CDT maps and lists are possible now, using Aerospike server version 4.6 or later and Aerospike Node.js client version 3.12 or later. That update introduced the withContext() function on list and map operations that lets you specify the context in which the list/map operation is to be executed. You can find more information in the documentation for the new CdtContext class.
Here is how you would perform the update given in your example:
const Aerospike = require('aerospike')
const maps = Aerospike.maps
Aerospike.connect().then(async (client) => {
const key = new Aerospike.Key('test', 'test', 'test')
const map = {
"a" : "apple",
"b" : "ball",
"c" : { "d" : "dog", "e" : "elephant" },
"f" : { "g" : { "h" : "horse" } },
"i" : { "j" : "jackal", "k" : { "l" : "lion", "m" : "monkey" } }
}
console.log('BEFORE:', map)
await client.put(key, map)
await client.operate(key, [
maps.put('i', 'n', 'nest')
.withContext((ctx) => ctx.addMapKey('k'))
])
const record = await client.get(key)
console.log('AFTER:', record.bins)
client.close()
}).catch((error) => {
console.error(error)
if (error.client) error.client.close()
})
Here is what you would get, when you run the example:
$ node nested-map-update-example.js
BEFORE: {
a: 'apple',
b: 'ball',
c: { d: 'dog', e: 'elephant' },
f: { g: { h: 'horse' } },
i: { j: 'jackal', k: { l: 'lion', m: 'monkey' } }
}
AFTER: {
a: 'apple',
b: 'ball',
c: { d: 'dog', e: 'elephant' },
f: { g: { h: 'horse' } },
i: { j: 'jackal', k: { l: 'lion', m: 'monkey', n: 'nest' } }
}
You will have to update the full value for the key "i".

Convert a list of maps into a nested map in groovy

I have a list of maps;
b = [{key1=hello, key2=world}, {key1=hello2, key2=world2}, {key1=hello3, key2=world3}, {key1=hello4, key2=world4}]
How do I convert this into something like this?
b = [{key1=hello, key2=world, {key1=hello2, key2=world2, {key1=hello3, key2=world3, {key1=hello4, key2=world4}}}}]
def a=[[key1:'hello', key2:'world'], [key1:'hello2', key2:'world2'], [key1:'hello3', key2:'world3']]
def b=a.reverse().inject(null){prev,item->
item.next = prev
return item
}
println new groovy.json.JsonBuilder(b).toPrettyString()
output:
{
"key1": "hello",
"key2": "world",
"next": {
"key1": "hello2",
"key2": "world2",
"next": {
"key1": "hello3",
"key2": "world3",
"next": null
}
}
}

C++ STL transform

I try to use c++ stl library.
I'd like to transform a vector of string to a map of int, string.
For example: a vector that looks like that:
const char *vinit[] = { "Sunday", "Monday", "Tuesday", "Wednesday" };
vector <string> v(vinit, end(vinit));
Will transform to a map like that:
map<int, string> map = {{ 1, "Sunday" },
{ 2, "Monday" },
{ 3, "Tuesday" },
{ 4, "Wednesday" } };
The point is that I can't use any loops and a global index as an integer.
[EDIT]
you can use map::insert for this:
const char *vinit[] = { "Sunday", "Monday", "Tuesday", "Wednesday" };
vector <string> v(vinit, end(vinit));
std::map<int,string> map;
for(int i = 0; i < v.size(); i++)
{
map.insert (std::pair<int,string>(i+1,v[i]));
}

Resources