Making Array of Hashes in Golang - arrays

I'm brand new to Go and having some trouble with nested data structures. Below is a array of hashes I mocked up that I need to make in Golang. I'm just confused with the whole having to declare the variable type beforehand and whatnot. Any ideas?
var Array = [
{name: 'Tom', dates: [20170522, 20170622], images: {profile: 'assets/tom-profile', full: 'assets/tom-full'}},
{name: 'Pat', dates: [20170515, 20170520], images: {profile: 'assets/pat-profile', full: 'assets/pat-full'}}
...,
... ]

What is called a 'hash' in Ruby is called a 'map' (translating keys to values) in Go.
However, Go is a statically typechecked language. A map can only map a certain type to another type, e.g. a map[string]int maps string values to integeger. That is not what you want here.
So what you want is a struct. Indeed, you need to define the type beforehand. So what you would do:
// declaring a separate 'Date' type that you may or may not want to encode as int.
type Date int
type User struct {
Name string
Dates []Date
Images map[string]string
}
Now that this type is defined, you can use it in another type:
ar := []User{
User{
Name: "Tom",
Dates: []Date{20170522, 20170622},
Images: map[string]string{"profile":"assets/tom-profile", "full": "assets/tom-full"},
},
User{
Name: "Pat",
Dates: []Date{20170515, 20170520},
Images: map[string]string{"profile":"assets/pat-profile", "full": "assets/pat-full"},
},
}
Note how we are defining User as a struct, but images as a map of string to image. You could also define a separate Image type:
type Image struct {
Type string // e.g. "profile"
Path string // e.g. "assets/tom-profile"
}
You would then not define Images as map[string]string but as []Image, that is, slice of Image structs. Which one is more appropriate depends on the use case.

You don't need to declare the variable type beforehand, at least not in this simple example, although you need to "mention" your types when initializing your values with composite literals.
For example this [{}] (array of objects?) makes no sense to the Go compiler, instead you need to write something like this []map[string]interface{}{} (slice of maps whose keys are strings and whose values can have any type)
To break it down:
[] - slice of whatever type comes after it
map - builtin map (think hash)
[string] - inside the square brackets is the type of the map key,
can be almost any type
interface{} - the type of the map values
{} - this initializes/allocate the whole thing
So your example in Go would look something like this:
var Array = []map[string]interface{}{
{"name":"Tom", "dates": []int{20170522, 20170622}, "images": map[string]string{"profile": "assets/tom-profile", "full": "assets/tom-full"}},
{"name":"Pat", "dates": []int{20170515, 20170520}, "images": map[string]string{"profile": "assets/pat-profile", "full": "assets/pat-full"}},
// ...
}
Read more on maps and what key types you can use here: https://golang.org/ref/spec#Map_types
That said, in Go, most of the time, you would first define your structured types more concretely and then use them instead of maps, so something like this makes more sense in Go:
type User struct {
Name string
Dates []int
Images Images
}
type Images struct {
Profile string
Full string
}
var Array = []User{
{Name:"Tom", Dates:[]int{20170522, 20170622}, Images:Images{Profile:"assets/tom-profile", Full:"assets/tom-full"}},
{Name:"Pat", Dates:[]int{20170515, 20170520}, Images:Images{Profile:"assets/pat-profile", Full:"assets/pat-full"}},
}

Related

Swift : multi-dimensional structure organization and init

There are two simple problems here in initializing this structure.
One is the enunumerated value TS (I get error : Cannot convert value of type 'TournNames' to expected argument type 'TournamentName')
the other is initializing an array of strings (I get the error : Cannot convert value of type '[String]' to expected argument type 'TouramentScores'
Suppose I am trying to set up a structure to model the scores of tennis players and all of their matches in each of the major tournaments (just for fun). Each tournament has a name (e.g. Wimbledon) and a series of scores for that player (for example, in the opening match, their score might be "4-6, 6-4, 7-6, 6-2")... upto 7 matches in each tournament. Each player should have an array of four tournaments (names and array of scores), and eventually there should be an array of players. I am also trying to use enums not too successfully. Ideally, if I want to find how Roger Federer did in his third match of wimbledon this year, I would access something like player.tournament.wim.Roundof32 or something roughly like that. But before I can even get to playing with that, I can't seem to init dummy data for even a single tournament.
Any ideas? I don't think this is that hard of question but I just don't know each. See "*** this line" below for two lines that are problematic
// tournament name enum
enum TournNames : String {
case wim = "Wimbledom"
case fo = "French Open"
case ao = "Australian Open"
case uo = "US Open"
}
//
struct TournamentName {
var Tname : TournNames // = .wim
}
// This is the structure for a tournament score array with some dummy values.
struct TouramentScores {
var Match1 : String = "7-6, 6-4, 3-6, 7-6"
var Match2 : String = "7-6, 6-4, 3-6, 7-6"
}
// This is one entire Tournament record for one player = tournament name + array of scores ... the next goal but not used here until I get over these hurdles
struct TournamentResult {
var TournamentName : TournNames = .wim
var Scores : TouramentScores
}
// ... finally the structure of a player ...
struct DummyTennisPlayer {
var LastName : String // last name
var FirstName : String //first name
var TN : TournamentName
var TS : TouramentScores
// var WimRes : TournamentResult // to start a single tournament
// var SeasonResults : [TournamentResult] // ultimately should be an array of 4 tournaments
}
// trying to initialize some dummy data without success after trying several things
extension DummyTennisPlayer {
static var dummyResults : [DummyTennisPlayer] {
[
DummyTennisPlayer.init(
LastName : "Federer",
FirstName: "Roger",
TN : TournNames.wim // **** this line
,
TS : ["XX", "yy"] /// *** this line
)
]
}
}
As I think you're discovering, a simple series of nested types is unlikely to cut it here. As soon as you get to entities like players, tournaments, matches and lookups like "how Roger Federer did in his third match of wimbledon this year", you've become a candidate for using a database where you can manipulate one-to-many and many-to-many relationships. I can't tell you what database to use, and anyway that's a matter of opinion; from what you've said so far, SQLite would be sufficient (and I am personally not a fan of Core Data just for this kind of thing).
I guess your code is a kind of exercise, so before you go on later to Core Data or SQLite,
extension DummyTennisPlayer {
static var dummyResults: [DummyTennisPlayer] = [
DummyTennisPlayer(LastName: "Federer", FirstName: "Roger", WimbledomResult: TournamentResult(Scores: TouramentScores()))
]
}
should answer your question.
1 - To initialize a Swift struct, use the following syntax:
MyStruct(property1: "my property1 value", property2: "my property2 value")
2 - the tournament name property in TournamentResult is already set to .wim so you just need to initialize the Scores. As your TournamentScores properties are already all set, you just need to pass an instance of TournamentScores() to TournamentResult(Scores:).
By the way, only use lowercases for the first letter of the name of your variables or struct properties: var lastName or TournamentResult(scores:).
I think you are confusing the term "multi-dimensional (array) structures" (which are just arrays nested inside other arrays, like that: [ [1, 2, 3], [2, 3, 4], [3, 4, 5]]) with the struct objects. You are probably not supposed to use structs so extensively here.
Don't hesitate to review the way you decide to use enums, structs, or arrays. Your code may work but will be difficult to read and use (example: how would you access a specific set score if you put all of the set scores in a single String? Why not use an array?)

Convert Array of objects to Array with only values

I have an array (obj_values) of objects
[
#<User id: 1, name: "Kostas">,
#<User id: 2, name: "Moufa">,
...
]
And I want to convert this into an Array with only the values from above objects, so it will look like:
[
1, Kostas
2, Moufa
]
I can do it like this:
obj_table = []
obj_values.each do |ext|
ext.each do |obj|
obj_table.push([obj.id, obj.name].join(","))
end
end
However with this approach I need to explicitly specify what attributes I want to push (obj.id and obj.name), is there a way to push whole data from object without the need to specify each attribute separately?
Use .attributes to get a hash of all the attributes on the model. Call .values on it to get just the values without the keys.
Using your code:
obj_table = []
obj_values.each do |ext|
ext.each do |obj|
obj_table.push(obj.attributes.values.join(","))
end
end
Though there are better ways. I suggest you look into .map and .flat_map.
obj_table = obj_values.flat_map do |ext|
ext.map do |obj|
obj.attributes.values.join(",")
end
end
Yes, you can push data without specifying it separately. to push data you have to use an object.attributes.values
an object will be your active record object.
attributes will return a hash with all attributes of that object.
so you can change your loops into a single statement like:
obj_values.flat_map {|object_value| object_value.attributes.values }

Mapping an array of classes to remove whitespace from values swift

I have a struct and an array of my structs as follows
struct Products{
var ProductType: String
var ProductName: String
var ProductLink: String
}
var CleaningProductsArray = [Products]()
When I write to my array of structs the ProductName Variable inside it sometimes can be written by the user with trailing whitespaces. I would like to return a version of the CleaningProductsArray but with all instances of ProductName having any trailing whitespaces removed. I have been trying to achieve with map as below but does not return what I would like it to. What is the most efficient way to do this?
let trimmed = CleaningProductsArray.map{ $0.ProductName.trimmingCharacters(in: .whitespaces) }
Quick answer is:
let trimmed: [Products] = CleaningProductsArray.map { product in
var adjusted = product
adjusted.ProductName = product.ProductName.trimmingCharacters(in: .whitespaces)
return adjusted
}
As it was correctly mentioned in the comments, there are things you can improve in your overall code design.
You could start with converting your model to meet Swift naming standards, which means not using plural for Products since the objects of this type describe a single product, and removing the product prefix from properties since its obvious from the context that they describe a "Product". Ideally you would also make the properties immutable, to make passing them around safer (google "Benefits of immutability"). You should create some other object responsible for collecting all the data for your product objects.
struct Product {
let type: String
let name: String
let link: String
}
Also, you should never use uppercased names for your variables/constants/properties/functions in Swift, so it's best to replace the CleaningProductsArray with cleaningProductsArray for the sake of readability. Uppercased names are reserved for types. Also you might want to drop the Array suffix since it's obvious from the type that it is an array
var cleaningProducts = [Product]()
let trimmed: [Product] = cleaningProducts.map {
Product(
type: $0.type,
name: $0.name.trimmingCharacters(in: .whitespaces),
link: $0.link
)
}

How to pull all objects from an array of unknown length and place them into a state variable in React?

I imported data from another file (so I technically think it's an object containing an array of objects) called { groupData }. At the moment, the array contains only 5 objects, but it is variable in length and could contain more. Each object looks like this:
{
name: "A Name",
img: "https://imgURL.goes.here",
details: "This is a fun group about fun things.",
likes: 45,
},
My goal is to take each object from the array, modify the data, and place the objects into an empty stateful array called "groups". I want each object to look like this before it goes into the new "groups" array:
{
name: "A Name",
img: "https://imgURL.goes.here",
details: "This is a fun group about fun things.",
hasNotification: Boolean,
userIsAdmin: Boolean,
},
I thought of destructuring the array, but this solution is not scalable if things are going to be added to the array:
const [groupZero, groupOne, groupTwo, groupThree, groupFour] = groupData;
What is the most efficient way to accomplish this? Thank you!!
I am not exactly sure what you need because of the lack of context. But you can use a for loop to iterate through each object in the array.
If it is fine to moderate the existing data:
for (data of groupData){
delete data.likes;
data.hasNotification = true; // or false
data.userIsAdmin = true; // or false
}
setState(groupData); // if you are using useState hooks
If you do not want to change original data:
// create a deep clone of groupData
const newGroup = JSON.parse(JSON.stringify(groupData));
for (data of newGroup){
delete data.likes;
data.hasNotification = true; // or false
data.userIsAdmin = true; // or false
}
setState(newGroup);
Adjust the setState section accordingly if you are using class components

Access a value in a dictionary using an int variable in Swift

I'm developing an iOS app and I want to access a specific value in a Dictionary using Array().
My dictionary contains an array, which contains structs.
let array = [(key: "S", value: [Thunderbolt.repoStruct(repoName: "Semiak Repo", repoURL: "https://repo.semiak.dev", icon: Optional("iconRound"))]), (key: "T", value: [Thunderbolt.repoStruct(repoName: "Thunderbolt iOS Utilities", repoURL: "https://repo.thunderbolt.semiak.dev", icon: Optional("iconRound"))])]
I'm making an UITableView with the array: the section name is the key value, the cell title is the repoStruct.repoName value, and the same with the following values.
To access repoName I'd use Array(array)[0].1[0].repoName.
The problem is that I do not know the exact location I want to access, instead, I use indexPath to know which value I need:
Array(array)[indexPath.section].indexPath.row[0].repoName
This should return me the repoName of the cell, but instead gives me the following error: Value of tuple type '(key: String, value: [repoStruct])' has no member 'indexPath'
I also tried using:
let row = indexPath.row
Array(array)[indexPath.section].row[0].repoName
but it gives me the same error: Value of tuple type '(key: String, value: [repoStruct])' has no member 'row'
I do not know why Array(array)[0].1 works and returns me the value, but Array(array)[indexPath.section].row doesn't. It is doing the same: accessing a value using the position, which is an int, such as indexPath.
How could I accomplish this?
Thanks in advance.
You are strongly discouraged from using tuples in a data source array. Replace the tuple with an extra struct
struct Section {
let name : String
let items : [Thunderbolt.repoStruct]
}
let array = [Section(name: "S", items: [Thunderbolt.repoStruct(repoName: "Semiak Repo", repoURL: "https://repo.semiak.dev", icon: Optional("iconRound"))],
Section(name: "T", items: [Thunderbolt.repoStruct(repoName: "Thunderbolt iOS Utilities", repoURL: "https://repo.thunderbolt.semiak.dev", icon: Optional("iconRound"))]]
and get an item at index path
let section = array[indexPath.section]
let item = section.items[indexPath.row]
let name = item.repoName
First of all, your array is already an array, so there's no need to say Array(array) - simply array will suffice, although generic names like this should be avoided.
I do not know why array[0].1[0] works
Let's pick this apart - your're accessing the first element in array via [0] and within that, the second element of the tuple .1, and lastly the first element of that valuearray. You could use array[0].value[0] for the same effect and make the code more readable.
but array[indexPath.section].row doesn't
That's because your array does not contain anything called row.
Use array[indexPath.section].value[indexPath.row].repoName instead.
Please try this code.
let dictData = arr[indexpath.section] //Element of section
let value = dictData["value"] //Value added in value in The element
let name = value[indexpath.row].reponame //Gives you name

Resources