Generate data for secionlists in react native - react-native-flatlist

The SectionList in react native requires the data feed like the following:
sections={[
{title: 'D', data: ['Devin']},
{title: 'J', data: ['Jackson', 'James', 'Jillian', 'Jimmy', 'Joel', 'John', 'Julie']},
]}
My understanding of data above is a object of object array. I can generate an object array like this:
let obj_arr = [];
obj1 = {
a: 1,
b: 2
};
obj_arr.push(obj1);
obj2 = {
c: 3,
b: 4
};
obj_arr.push(obj2);
How to generate object for object array of obj_arr?

Related

How to insert a list into a map?

I have 2 data one in an array and the other in a list. I need to send these data in API but combine them.
My first data look like this:
data1 = {vendorId: 17, vendorName: Dolphin Bakers};
and second is like this
data2 = [
{id: 3, province: and, code: 56201},
{id: 3, province: and, code: 56201},
];
I need to send it like this
{
vendorId: 17,
vendorName: Dolphin Bakers,
data: [
{id: 3, province: and, code: 56201},
{id: 3, province: and, code: 56201},
],
}
I am trying to add second data in the first data
data1.add(data: data2);
Its showing error Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'add'.
You must put a "data" key in the map:
data1["data"] = data2;

Dart - Flatten a list while removing duplicate values

So, I have a list of lists, like this
var a = [['a', 'b', 'c'], ['1', '2', 'b'], [3, 'd', true]];
I want to output a flattened list with duplicate values removed -
var b = ['a', 'b', 'c', '1', '2', 3, 'd', true];
While, I know I can use map/reduce, just trying to understand what is the most dart like way of doing this.
More info about input - I'm just trying to understand iterables and sets better in Dart (coming from Python world). Dart Trickeristry welcome!
You want to flatten the list of lists and remove duplicates.
The way to remove duplicates is to use a Set with an efficient lookup (or something equivalent, but Set is the simplest de-duplicating functionality).
Any other approach is likely going to be quadratic because it checks each element against each other element.
For that, I'd do:
var result = [...{for (var list in lists) ...list}];
This flattens the lists into a set, then converts that set to a list again.
The one other alternative would be flattening into a list, sorting the list, and then removing adjacent duplicates. That requires the elements to be Comparable, which the example given here isn't, or that you provide a comparison function. Let's assume the latter:
List<T> flatUnique(Iterable<Iterable<T>> elements, int compare(T a, T b)) {
var flat = [for (var element in elements) ...element];
if (flat.isEmpty) return flat;
flat.sort(compare);
var current = flat[0];
var j = 1;
for (var i = 1; i < flat.length; i++) {
var next = flat[i];
if (current != next) {
current = flat[j++] = next;
}
}
flat.length = j;
return flat;
}
That's more complicated because there is not a general "remove duplicates from sorted list" operation in the libraries ... because people just use a set.
It also changes the order of elements other than by removing duplicates.
in dart 2.3,
you can actually use spread operator like this:
var a = [['a', 'b', 'c'], ['1', '2', 'b'], [3, 'd', true]];
var b = [...a[0], ...a[1], ...a[2]];
// print ['a', 'b', 'c', '1', '2', 3, 'd', true];
and do your code afterwards
You can combine a Set with the expand method.
var a = [['a', 'b', 'c'], ['1', '2', 'b'], [3, 'd', true]];
var b = Set.from(a.expand((x) => x)).toList();
// [a, b, c, 1, 2, 3, d, true]

Flutter - Search elements in array with firstwhere

The problem is simple: What is the correct way to search element in array ?
My code is
data = [{id: 1, descripcion: Asier}, {id: 2, descripcion: Pepe}]
estateSelected= data.firstWhere((dropdown)=>dropdown.id==1);
The error that return is
Bad state: no element
You have some errors, this should work:
var data = [{'id': 1, 'descripcion': 'Asier'}, {'id': 2, 'descripcion': 'Pepe'}];
var estateSelected = data.firstWhere((dropdown) => dropdown['id'] == 1);
print(estateSelected);
Fastest way to try is on dartpad

Duplicate map is being added to list

void main() {
Map firstMap = { "id": 1, "value": [{"key": "A"}, {"key": "B"}]};
List<Map> newList = [];
List valueList = firstMap["value"];
for(int y=0; y<valueList.length; y++) {
Map newMap = firstMap;
newMap["value"] = [{"key": valueList[y]["key"]}];
newList.add(newMap);
}
print(newList);
}
My expectation for newList is to look like:
[{id: 1, value: [{key: A}]}, {id: 1, value: [{key: B}]}]
but the actual value of newList is
[{id: 1, value: [{key: B}]}, {id: 1, value: [{key: B}]}]
I am not sure what to try here.
valueList[y]["key"] seems to have the correct value in each iteration.
The code above has been shared as a link here to dart pad.
How would I achieve the same? Also what is going on with my code, which is leading to such a result.
Open with Dart pad
Error that you are doing is as already mentioned in the comment, reassignment to different variable does not make a copy of data:
Map a = {"a": 1}
Map b = a; // b is just one more reference to same object
b["a"] = 2;
print(a); // -> {"a": 2}
Transformations from one shape to another can be conveniently done with map()
https://dartpad.dartlang.org/8283e91c2c201ddb65ef073e76995f1d
void main() {
Map firstMap = { "id": 1, "value": [{"key": "A"}, {"key": "B"}]};
List valueList = firstMap["value"];
List<Map> newList = valueList.map((valueItem) => {
"id": firstMap["id"],
"value": [valueItem]
}).toList();
print(newList);
}

Is it possible to specify categories in point objects for column charts in highcharts?

I'm trying to specify column charts with objects for points. The reason for this is that I don't have to then split up the array of objects into separate arrays of categories and values.
I have tried the following, but neither works. (I'm interested in both specifying the category for columns, and specifying datetimes for x values.)
var data_1 = [
{x:new Date('2013-01-03'),y:2},
{x:new Date("2013-01-02"),y:6},
{x:new Date("2013-01-01"),y:4},
];
var data_2 = [
{category: "a", y: 3},
{category: "b", y: 3},
{category: "c", y: 4}
];
var create_chart = function(el, data){
var init_obj = {
chart: {
type: 'column'
},
series: [{
name: 'a',
data: data
}]
};
el.highcharts(init_obj);
};
create_chart($('#chart1'), data_1);
create_chart($('#chart2'), data_2);
In highcharts configuration you can set onyl one type of one xAxis. If xAxis has datetime type, you cannot use categoires. Only wayt o common both types is use two xAxis with different types.

Resources