I would like to create an array of dates (or of tuples including an index and a data) from the following JSON.
My code is creating an array but instead of creating an array of dates, it breaks up the dates into characters. What do I need to do to create an array just of dates.
JSON looks like:
let json = """
[{"date":"2017-01-05",
"price":119.34},{"date":"2017-01-06",
"price":118.93}];
Code is:
let myprices = try JSONDecoder().decode([Prices].self, from: Data(json.utf8))
let dates = myprices.sorted{$0.date < $1.date}.enumerated().map {Array($0.element.date)}
Code prints to console as:
dates [["2", "0", "1", "7", "-", "0", "1", "-", "0", "5"], ["2", "0", "1", "7", "-", "0", "1", "-", "0", "6"], ["2", "0", "1", "7", "-", "0", "1", "-", "0"]]
Thanks in advance for any suggestions.
Replace
let dates = myprices.sorted{$0.date < $1.date}.enumerated().map {Array($0.element.date)}
with
let dates = myprices.sorted{$0.date < $1.date}.map { $0.date }
Currently you may be making let data:String change it to let date:Date and supply a formatter to the decoder check This
Related
I have two type of list.
Here,
List list1= [
{"id": "3", "topic_name": "Studying In Bangladesh"},
{"id": "4", "topic_name": "Studying In Sweden"},
{"id": "6", "topic_name": "Studying In Germany"},
];
List list2 = [
"2",
"3",
"5",
"6",
];
I want to generate a new topic list from list1, which is matched by list2's elements ( with list1's id ).
Is there any method are available for dart ?
You can do this:
List list3 = list1.where((e) => list2.contains(e['id'])).toList();
You can compare list1's id element with the list 2 with the foreach property in list for iterate through all elements of the list
How can I convert a map like
{
"name": "my name",
"date": "14 Feb 2020",
"days": "15",
}
Into something like
[
"my name",
"14 Feb 2020"
"15"
]
Basically converting map values into List.
I've tried
list.add(name) and so on
but there will be problems if there is many data. Is there any way to iterate to get the expected result?
You can get a value list using the values getter.
final map = {
"name": "my name",
"date": "14 Feb 2020",
"days": "15",
};
map.values.toList(); // <---- Here
Map myMap = {"name":"mike", "age":"12};
List myList = myMap.values.toList();
I am trying to use flextable to make a nice table that knits into word, with descriptive banners/headers through out
Example data:
trialdata<-structure(list(` ` = c("Number per team", "Average height", "vegetarian",
"meat", "carrot", "cucumber", "orange",
"banana", "pepper", "tangerine", "Average Score",
"Range Score", "Number of children", "Number of parents",
"Number of grandparents"), `year 1` = c("20", "2",
"25", "12", "4", "7",
"7", "37", "21", "3",
"-0.3", "78 : 1", "61", "19",
"39"), `Year 2` = c("98", "28.2", "23",
"1", "8", "6", "1",
"36", "2", "29", "-0.2", "3 : 2",
"6", "18", "9"), `Year 3` = c("88",
"28.2", "24", "1", "1", "4",
"91", "3", "24 ", "2",
"-0.2", "7 : 2", "58", "1",
"8")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-15L))
Make a table:
install.packages("flextable")
library(flextable)
table1<-flextable(trialdata)
That makes a table which looks like the below picture, with a single header
However I would like to add some kind of descriptive header, e.g.'favorite food'. This is not a 'grouping' already in the data, but an added text I would like to make the table clearer, for example to look like this:
Flex table has ways to add headers and footers, but I can't see a way to add a header-type object in the table.
When I construct 2D array as follows, and apply flatMap with a range on it, I get the following result:
var a = [["5", "3", ".", ".", "7", "."],["6",".",".","1","9","5"]]
print(a.flatMap{$0[1..<5]})
output:
["3", ".", ".", "7", ".", ".", "1", "9"]
But if I want to just display the range as follows, I am getting the following error.
print(a[1..<5])
Terminated by signal 4
The crash is exactly what one would expect. a has just two elements, indexed as 0 and 1. Applying a larger index by saying a[1..<5] (asking also for elements 2, 3, and 4) puts you out of range.
If you think of the elements in the range as columns of the 2D Array, then you could get them this way:
let a = [["5", "3", ".", ".", "7", "."],
["6", ".", ".", "1", "9", "5"]]
func getColumns<T>(in range: Range<Int>, from array2d: [[T]]) -> [T] {
return array2d.flatMap { $0[range] }
}
getColumns(in: 1..<5, from: a) //["3", ".", ".", "7", ".", ".", "1", "9"]
Bear in mind that this could still throw an Index out of range error just like with any other array.
I have some JSON data that I am getting from my database. I can pull it fine and load it into my table view. my issue is separating my JSON data so I can section the tableview. I have JSON array like this
"data": [
{
"ID": 1,
"wilayah_id": 1,
"name": "Jembatan Lima",
"region_name": "region1"
},
{
"ID": 2,
"wilayah_id": 1,
"name": "Kebon Jeruk",
"region_name": "region1"
},
{
"ID": 18,
"wilayah_id": 3,
"name": "Waylunik",
"region_name": "region2"
},
{
"ID": 19,
"wilayah_id": 3,
"name": "Tenggiri",
"region_name": "region2"
},
{
"ID": 25,
"wilayah_id": 3,
"name": "Mesuji",
"region_name": "region3"
},
{
"ID": 26,
"wilayah_id": 4,
"name": "KM 6",
"region_name": "region3"
}
]
What I'm trying to do is separate this data into three parts sort by "region_name" in my table view.
//region model
struct RegionList {
var ID:String
var wilayah_id:String
var name:String
var region_name:String
static var dataSource:[RegionList] {
return [
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R1"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R2"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R1"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R2"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R3")
]
}
}
//Coming Json Array
var arrRegion:[RegionList] = []
//MAKe Section Array by Using Model Class
var regionInSectionArray:[[RegionList]] = []
//Using Dic key in String and value is Model
var usingDicKeySectionArray:[String:[RegionList]] = [:]
// Dummy Data
arrRegion = RegionList.dataSource
//Using Default Dictionary Method to make a group
usingDicKeySectionArray = Dictionary.init(grouping: arrRegion, by: { (region) -> String in
/// Here we are making group dictionary by using region name, you can set by own requirment
//Specify by Region
return region.region_name
})
// You can use it in number of section usingDicKeySectionArray.count
//And number of rowInSection usingDicKeySectionArray[section].count
print(usingDicKeySectionArray.map{$0.key})
//end
///Sorting Keys
//Second option
let sortedKeys = usingDicKeySectionArray.keys.sorted()
sortedKeys.forEach { (key) in
//
let region = usingDicKeySectionArray[key]
regionInSectionArray.append(region ?? [])
}
//USe
//regionInSectionArray.count // in numberOfSection
//regionInSectionArray[section].count // in numberOfRowInSection
//result = ["R1", "R2", "R3"]