I have an array like this:
[
{:game_id=>546012, :period=>:fg, :stat_name=>:hits, :result=>12, :team_id=>1104},
{:game_id=>546012, :period=>:fg, :stat_name=>:errors, :result=>1, :team_id=>1104},
{:game_id=>546012, :period=>:fg, :stat_name=>:hits, :result=>9, :team_id=>1103},
{:game_id=>546012, :period=>:fg, :stat_name=>:errors, :result=>3, :team_id=>1103}
]
How can turn it into an array of items like this:
{ 546012 => { :hits => { :fg => { 1104 => 12,
1103 => 9 } }
:errors => { :fg => { 1104 => 1,
1103 => 3 } } }
First, create a hash organized by :game_id
{ 546012 => {{
{:period=>:fg, :stat_name=>:hits, :result=>12, :team_id=>1104},
{:period=>:fg, :stat_name=>:errors, :result=>1, :team_id=>1104},
{:period=>:fg, :stat_name=>:hits, :result=>9, :team_id=>1103},
{:period=>:fg, :stat_name=>:errors, :result=>3, :team_id=>1103}
]}
Then group by :stat_name
{ 546012 => [
{:hits => [
{:period=>:fg, :result=>12, :team_id=>1104},
{:period=>:fg, :result=>9, :team_id=>1103}],
:errors => [
{:period=>:fg, :result=>1, :team_id=>1104},
{:period=>:fg, :result=>3, :team_id=>1103}
]}
]}
Then group by period:
{ 546012 => [
{:hits => [
:fg => [{:result=>12, :team_id=>1104},
{:result=>9, :team_id=>1103}]
]}
]},
:errors => [
:fg => [{:result=>1, :team_id=>1104},
{:result=>3, :team_id=>1103}]
]}
]}
Lastly, group by :team_id and associate each one to its :result.
{ 546012 => [
{:hits => [
:fg => [1104 => 12, 1103 => 9]
]},
:errors => [
:fg => [1104 => 1, 1103 => 3]
]}
]}
As for how to create these groupings, I will leave it as an exercise to you.
One possible method is to loop through each item and create a new copy of the object that stores these new mappings. For instance, if we have this object:
foods = {
{:food => lemon, :taste => sour},
{:food => pretzel, :taste => salty},
{:food => pretzel, :taste => sweet}
}
We could group by foods like (pseudocode):
newfoods = {};
foreach item in foods:
newfoods[ item.food ].push( item.taste );
And end up with
newfoods = [
{ lemon => [sour] },
{ pretzel => [sweet, salty] }
]
The best option you can do is flatten it.
Credits go to this question.
Related
I am looping the database to get a list of countries with continents.
array: [
0 => array: [
"country" => "BE"
"continent" => "EU"
1 => array: [
"country" => "BG"
"continent" => "EU"
]
...
]
From that result, I want to create an array that shows all the continents with the countries inside.
array:[
0 => array: [
"continent" => "EU"
"countries" => [
"country" => "BE"
"country" => "BG"
]
]
Suppose this is your array
$arr = [
[
"country" => "BE",
"continent" => "EU",
],
[
"country" => "BG",
"continent" => "EU",
]
];
Then this returns what you expected.
collect($arr)->groupBy('continent')->mapWithKeys(function ($group, $continent) {
return [
'continent' => $continent,
'countries' => $group->pluck('country')->toArray()
];
});
If you need just to group countries by continent, simple way to achieve this by mapToGroups() method for collections
$input = [
['country' => 'BE', 'continent' => 'EU'],
['country' => 'BG', 'continent' => 'EU'],
['country' => 'BU', 'continent' => 'AF'],
['country' => 'BY', 'continent' => 'EU'],
];
$grouped = collect($input)
->mapToGroups(fn ($country) => [$country['continent'] => $country['country']])
->toArray(); // if you need array at the end
This will be resulted in
You will need to some how group your results by the continent. One way to do that is to loop over the initial collection and build a new array keyed by the continent.
However, seeing that you are using Laravel just group the collection by the continent.
$countries = [
['country' => 'A', 'continent' => 'aa'],
['country' => 'B', 'continent' => 'bb'],
['country' => 'C', 'continent' => 'aa'],
['country' => 'D', 'continent' => 'aa'],
['country' => 'E', 'continent' => 'aa'],
['country' => 'F', 'continent' => 'bb'],
];
// Because my data is an array I just turn that into a collection.
// But an Eloquent query builder ->get would already return a collection.
$continents = collect($countries)->groupBy('continent');
foreach ($continents as $continent => $items) {
echo "Countries for " . $continent . "\n";
foreach ($items as $country) {
echo $country['country'] . "\n";
}
}
/**
Output:
Countries for aa
A
C
D
E
Countries for bb
B
F
**/
I am trying to install the Keycloak with SQL databse server by using docker-compose file. IT run but keycloak stop with following error.
"ssl-context" => "applicationSSC",
"proxy-address-forwarding" => expression "${env.PROXY_ADDRESS_FORWARDING:false}",
"enable-http2" => true
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("server" => "default-server"),
("host" => "default-host")
],
"alias" => ["localhost"]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("server" => "default-server"),
("host" => "default-host"),
("location" => "/")
],
"handler" => "welcome-content"
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("server" => "default-server"),
("host" => "default-host"),
("setting" => "http-invoker")
],
"http-authentication-factory" => "application-http-authentication"
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("servlet-container" => "default")
]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("servlet-container" => "default"),
("setting" => "jsp")
]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("servlet-container" => "default"),
("setting" => "websockets")
]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("configuration" => "handler"),
("file" => "welcome-content")
],
"path" => expression "${jboss.home.dir}/welcome-content"
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("application-security-domain" => "other")
],
"security-domain" => "ApplicationDomain"
}, {
"operation" => "add",
"address" => [("subsystem" => "weld")]
}, {
"operation" => "boottime-controller-initializer-step",
"address" => []
}, {
"operation" => "add-deployer-chains",
"address" => []
}]: java.util.concurrent.RejectedExecutionException
at org.jboss.threads#2.4.0.Final//org.jboss.threads.RejectingExecutor.execute(RejectingExecutor.java:37)
at org.jboss.threads#2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.rejectShutdown(EnhancedQueueExecutor.java:2029)
at org.jboss.threads#2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.execute(EnhancedQueueExecutor.java:757)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.notification.NotificationSupports$NonBlockingNotificationSupport.emit(NotificationSupports.java:95)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.OperationContextImpl.notifyModificationsComplete(OperationContextImpl.java:517)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.OperationContextImpl.releaseStepLocks(OperationContextImpl.java:1245)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext$Step.finalizeInternal(AbstractOperationContext.java:1534)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext$Step.finalizeStep(AbstractOperationContext.java:1492)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext$Step.access$400(AbstractOperationContext.java:1356)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext.executeResultHandlerPhase(AbstractOperationContext.java:910)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext.processStages(AbstractOperationContext.java:790)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext.executeOperation(AbstractOperationContext.java:466)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.OperationContextImpl.executeOperation(OperationContextImpl.java:1427)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.ModelControllerImpl.boot(ModelControllerImpl.java:559)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractControllerService.boot(AbstractControllerService.java:572)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractControllerService.boot(AbstractControllerService.java:534)
at org.jboss.as.server#18.1.0.Final//org.jboss.as.server.ServerService.boot(ServerService.java:470)
at org.jboss.as.server#18.1.0.Final//org.jboss.as.server.ServerService.boot(ServerService.java:414)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractControllerService$1.run(AbstractControllerService.java:473)
at java.base/java.lang.Thread.run(Thread.java:829)
Suppressed: java.util.concurrent.RejectedExecutionException: Executor is being shut down
at org.jboss.threads#2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.rejectShutdown(EnhancedQueueExecutor.java:2031)
... 18 more
rUser with username 'admin' already added to '/opt/jboss/keycloak/standalone/configuration/keycloak-add-user.json'
I have search other tickets but no able to fix this issue. Username is not an issue I have delete all and try again, not able to run.
My Docker compose file is following
version: "3.2"
services:
mssql:
image: mcr.microsoft.com/mssql/server
ports:
- "1433:1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=Password!23
- MSSQL_PID=Developer
mssqlscripts:
image: mcr.microsoft.com/mssql-tools
depends_on:
- mssql
command: /bin/bash -c 'until /opt/mssql-tools/bin/sqlcmd -S mssql -U sa -P "Password!23" -Q "create database Keycloak"; do sleep 5; done'
keycloak:
image: quay.io/keycloak/keycloak:legacy
depends_on:
- mssql
- mssqlscripts
ports:
- "8443:8080"
environment:
- KEYCLOAK_USER=admin
- KEYCLOAK_PASSWORD=admin
- DB_VENDOR=mssql
- DB_USER=sa
- DB_PASSWORD=Password!23
- DB_ADDR=mssql
- DB_DATABASE=Keycloak
When I try to comment the user name and Pass
"socket-binding" => "https",
"ssl-context" => "applicationSSC",
"proxy-address-forwarding" => expression "${env.PROXY_ADDRESS_FORWARDING:false}",
"enable-http2" => true
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("server" => "default-server"),
("host" => "default-host")
],
"alias" => ["localhost"]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("server" => "default-server"),
("host" => "default-host"),
("location" => "/")
],
"handler" => "welcome-content"
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("server" => "default-server"),
("host" => "default-host"),
("setting" => "http-invoker")
],
"http-authentication-factory" => "application-http-authentication"
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("servlet-container" => "default")
]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("servlet-container" => "default"),
("setting" => "jsp")
]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("servlet-container" => "default"),
("setting" => "websockets")
]
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("configuration" => "handler"),
("file" => "welcome-content")
],
"path" => expression "${jboss.home.dir}/welcome-content"
}, {
"operation" => "add",
"address" => [
("subsystem" => "undertow"),
("application-security-domain" => "other")
],
"security-domain" => "ApplicationDomain"
}, {
"operation" => "add",
"address" => [("subsystem" => "weld")]
}, {
"operation" => "boottime-controller-initializer-step",
"address" => []
}, {
"operation" => "add-deployer-chains",
"address" => []
}]: java.util.concurrent.RejectedExecutionException
at org.jboss.threads#2.4.0.Final//org.jboss.threads.RejectingExecutor.execute(RejectingExecutor.java:37)
at org.jboss.threads#2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.rejectShutdown(EnhancedQueueExecutor.java:2029)
at org.jboss.threads#2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.execute(EnhancedQueueExecutor.java:757)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.notification.NotificationSupports$NonBlockingNotificationSupport.emit(NotificationSupports.java:95)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.OperationContextImpl.notifyModificationsComplete(OperationContextImpl.java:517)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.OperationContextImpl.releaseStepLocks(OperationContextImpl.java:1245)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext$Step.finalizeInternal(AbstractOperationContext.java:1534)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext$Step.finalizeStep(AbstractOperationContext.java:1492)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext$Step.access$400(AbstractOperationContext.java:1356)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext.executeResultHandlerPhase(AbstractOperationContext.java:910)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext.processStages(AbstractOperationContext.java:790)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractOperationContext.executeOperation(AbstractOperationContext.java:466)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.OperationContextImpl.executeOperation(OperationContextImpl.java:1427)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.ModelControllerImpl.boot(ModelControllerImpl.java:559)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractControllerService.boot(AbstractControllerService.java:572)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractControllerService.boot(AbstractControllerService.java:534)
at org.jboss.as.server#18.1.0.Final//org.jboss.as.server.ServerService.boot(ServerService.java:470)
at org.jboss.as.server#18.1.0.Final//org.jboss.as.server.ServerService.boot(ServerService.java:414)
at org.jboss.as.controller#18.1.0.Final//org.jboss.as.controller.AbstractControllerService$1.run(AbstractControllerService.java:473)
at java.base/java.lang.Thread.run(Thread.java:829)
Suppressed: java.util.concurrent.RejectedExecutionException: Executor is being shut down
at org.jboss.threads#2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.rejectShutdown(EnhancedQueueExecutor.java:2031)
... 18 more
experience_values = ["expert"]
#listing_data =
{
:image_url => "/images/original/missing.png",
:expertise => "Expert",
:skills => [
[0] "Default category"
],
:id => "d8nVaHWz2oP4ki8ayT9Nog",
:given_name => "Ritik",
:family_name => "Som",
:work_domain => "it ",
:languages => "english",
:description => "engineer",
:location => ""
},
[1] {
:image_url => "/images/original/missing.png",
:expertise => "Beginner",
:skills => [
[0] "Default category"
],
:id => "mkfVPmAWHbMG6MT8rp_KKw",
:given_name => "Ritik",
:family_name => "Som",
:work_domain => "",
:languages => "",
:description => "",
:location => ""
}
ll = []
#listing_data.each do |lal|
experience_values.each do |ee|
# binding.pry
if lal[:expertise].include? ee
ll.push(#listing_data)
end
end
end
ll = ll.uniqll = []
#listing_data.each do |lal|
experience_values.each do |ee|
# binding.pry
if lal[:expertise].include? ee
ll.push(#listing_data)
end
end
end
ll = ll.uniq
When I am doing the above code its adding both the element if ll array but only first one contains expert.Someone please help.I am stuck and not able to move ahead.I am working on ruby on rails for quite often.
Thanks in advance.
Try with below code
looks like listing_data is array of hash
experience_values = ["expert"]
listing_data = [{
:image_url => "/images/original/missing.png",
:expertise => "Expert",
:skills => ["Default category"],
:id => "d8nVaHWz2oP4ki8ayT9Nog",
:given_name => "Ritik",
:family_name => "Som",
:work_domain => "it ",
:languages => "english",
:description => "engineer",
:location => ""
},{
:image_url => "/images/original/missing.png",
:expertise => "Beginner",
:skills => ["Default category"],
:id => "mkfVPmAWHbMG6MT8rp_KKw",
:given_name => "Ritik",
:family_name => "Som",
:work_domain => "",
:languages => "",
:description => "",
:location => ""
}]
listing_data.select {|h| experience_values.include? h[:expertise].downcase }
out put
[{:image_url=>"/images/original/missing.png", :expertise=>"Expert", :skills=>["Default category"], :id=>"d8nVaHWz2oP4ki8ayT9Nog", :given_name=>"Ritik", :family_name=>"Som", :work_domain=>"it ", :languages=>"english", :description=>"engineer", :location=>""}]
I have an array A that looks like this:
A = [ { "id" => "1234", "name" => "audi", "isCool" => false },
{ "id" => "5678", "name" => "acura", "isCool" => false },
{ "id" => "9101112", "name" => "bentley", "isCool" => true },
{ "id" => "13141516", "name" => "rollsroyce", "isCool" => true },
{ "id" => "17181920", "name" => "toyota", "isCool" => true } ]
and I have an array B that looks like this:
B = ["1234", "13141516”]
I am trying to select only elements from array A that match array A's ids with Array Bs elements.
So the returned results I would like is:
C = [ { "id" => "1234", "name" => "audi", "isCool" => false },
{ "id" => "13141516", "name" => "rollsroyce", "isCool" => true } ]
Is there an easy way to go about this?
I have currently tried this but obviously not a good idea:
a.select {|x| x['id'] == B.first || B.last}
But obviously this is not dynamic, because what if I had 3 or 4 elements in array B.
A.select { |x| B.include?(x['id']) }
I'm having a problem trying to get objects from an array where a given hash might either have a specific property and a specific value, or a nested hash which potentially can too.
Is there a method for returning the specific hash that has the key I need OR RECURSE when it doesn't?
Example: I have this completely made-up structure:
the_array = [
{
:is_father => true,
:seek_this => "01"
},
{
:is_uncle => false,
:children => [
{
:seek_this => "09"
},
{
:seek_this => "2a"
}
]
},
{
:random_property=> 3,
:children => {
:random_er => true,
:children => [
{
:is_father => false,
:children => [
{
:seek_this => "3b"
},
{
:seek_this => "h1"
}
]
}
]
}
}
]
And after calling
the_array
.methodThatIDoNotKnow { |x| !x.seek_this.nil? }
.each do |hash_i_need|
//operate on hash somehow
hash_i_need.seek_this = 0xDEADBEEF
end
This is what I would expect to have happened:
the_array = [
{
:some_key => true,
:seek_this => 0xDEADBEEF
},
{
:some_other_key => false,
:children => [
{
:seek_this => 0xDEADBEEF
},
{
:seek_this => 0xDEADBEEF
}
]
},
{
:random_key: 3,
:children => {
:random_er => true,
:children => [
{
:is_father => false,
:children => [
{
:seek_this => 0xDEADBEEF
},
{
:seek_this => 0xDEADBEEF
}
]
}
]
}
}
]
I understand this is something that I can code myself, I'm just wondering if I need to or there is functionality for this kind of search out of the box.
Thanks!
there is a fetch method on hash but it doesn't recurse through object
you could use this answer or there are many deep fetch examples out there. good luck!