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']) }
Related
This is the rquest body:
{
"arr": [
{
"a": 3,
"b": 3
},
{
"a": 3,
"b": 3
}
]
}
and with this validations rules:
public function rules()
{
return [
// 'arr' => 'array',
'arr.*.a' => 'required'
];
}
The result of dd($request->validated()); is what I want, I don't need b to be included
"arr" => array:2 [
0 => array:1 [
"a" => 3
]
1 => array:1 [
"a" => 3
]
]
But when I want to validate the array itself like this:
public function rules()
{
return [
'arr' => 'array',
'arr.*.a' => 'required'
];
}
b will be added to the output
"arr" => array:2 [
0 => array:1 [
"a" => 3,
"b" => 3
]
1 => array:1 [
"a" => 3,
"b" => 3
]
]
How can I prevent adding b to the dd($request->validated()); while validating the array itself?
I request a web service and get a JSON response:
{
"timestamp" : "2019-06-11T08:04:35Z",
"version" : "0.5",
"document" : [
{
"href" : "http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239",
"item" : [
{
"href" : "http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239",
"label" : "40 623 a",
"id" : "http://uri.gbv.de/document/opac-de-7:epn:3421084610",
"available" : [
{
"service" : "presentation"
}
],
"unavailable" : [
{
"service" : "loan"
},
{
"service" : "interloan"
}
]
},
{
"href" : "http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239",
"label" : "40 623 b",
"id" : "http://uri.gbv.de/document/opac-de-7:epn:342108467X",
"available" : [
{
"service" : "presentation"
}
],
"unavailable" : [
{
"service" : "loan"
},
{
"service" : "interloan"
}
]
}
],
"id" : "http://uri.gbv.de/document/opac-de-7:ppn:1629107239"
}
],
"institution" : {
"href" : "http://www.sub.uni-goettingen.de",
"content" : "Niedersächsische Staats- und Universitätsbibliothek Göttingen",
"id" : "http://uri.gbv.de/organization/isil/DE-7"
}
}
Then I parse the JSON with Perls Dumper::Data module:
my $data = decode_json($resultJson);
print Dumper($data);
It looks like:
$VAR1 = {
'document' => [
{
'item' => [
{
'available' => [
{
'service' => 'presentation'
}
],
'id' => 'http://uri.gbv.de/document/opac-de-7:epn:3421084610',
'href' => 'http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239',
'label' => '40 623 a',
'unavailable' => [
{
'service' => 'loan'
},
{
'service' => 'interloan'
}
]
},
{
'available' => [
{
'service' => 'presentation'
}
],
'unavailable' => [
{
'service' => 'loan'
},
{
'service' => 'interloan'
}
],
'id' => 'http://uri.gbv.de/document/opac-de-7:epn:342108467X',
'label' => '40 623 b',
'href' => 'http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239'
}
],
'id' => 'http://uri.gbv.de/document/opac-de-7:ppn:1629107239',
'href' => 'http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239'
}
],
'institution' => {
'content' => "Nieders\x{e4}chsische Staats- und Universit\x{e4}tsbibliothek G\x{f6}ttingen",
'id' => 'http://uri.gbv.de/organization/isil/DE-7',
'href' => 'http://www.sub.uni-goettingen.de'
},
'timestamp' => '2019-06-11T08:04:35Z',
'version' => '0.5'
};
I would like to display the availability of each item, but I'm having a hard time iterating through the arrays inside the objects. My actual code looks like:
my $availability = $data->{document}[0]->{item}[0]->{available};
foreach my $key (#{$availability}) {
if (($key->{'service'}) eq "loan") {
print $rueckgabe = "Loan: available.\n";
} elsif (($key->{'service'}) eq "presentation") {
print $rueckgabe = "Presentation: available.\n";
} elsif (($key->{'service'}) eq "interloan") {
print $rueckgabe = "ILL: available.\n";
}
}
My expected result would be:
Item 1: Loan: available.
Item 2: Loan: available.
Iterate over the items, for each item, iterate over the availabilities.
for my $i (0 .. $#{ $data->{document}[0]{item} }) {
print 'Item ', $i + 1, ': ';
for my $availability ($data->{document}[0]{item}[$i]{available}) {
for my $key (#$availability) {
print { interloan => 'ILL' }->{ $key->{service} } || ucfirst $key->{service},
": available\n";
}
}
}
I have the following request;
Collection {#278
#items: array:3 [
0 => array:2 [
0 => array:8 [
"id" => 631
"name" => "OIL, FILTER O.E.M."
"partno" => "235-00"
"oemnumber" => "16099 003"
"stock" => 0
"price" => "30"
"qty" => 1
"total" => 30
]
1 => array:8 [
"id" => 23
"name" => "SPEEDOMETER"
"partno" => "122-"
"oemnumber" => "25005 1013"
"stock" => 0
"price" => "276"
"qty" => 1
"total" => 276
]
]
1 => array:2 [
0 => array:2 [
"description" => "Oil change"
"hours" => "1"
]
1 => array:2 [
"description" => "Tune up"
"hours" => "2"
]
]
2 => array:15 [
"id" => 1
"custId" => 9046
"bikeId" => 5238
"trans" => "yes"
"transDetails" => "call cab"
"policies" => "Yes"
"locker" => "1"
"lockerContents" => "stuff"
"estimate" => "Yes"
"oldparts" => "Yes"
"status" => "Pending"
"created_by" => null
"created_at" => "2016-05-19 14:40:59"
"updated_by" => null
"updated_at" => "2016-06-08 09:06:58"
]
]
}
I am getting this through;
$collection = collect($request->all());
How should I go about accessing the attributes in these arrays? I have tried pluck with no joy. I suspect I could do a loop over them but with no array_expression for the arrays do I need to use the index?
I have an array that stores data similarly to this:
people = [
{
'name' => "Jim",
'car' => "Porche",
'houseSize' => "big",
},
{
'name' => "Bill",
'car' => "Honda",
'houseSize' => "small",
}
]
How would I go about deleting an item in the array based off of a key in the dictionary.
i.e. If I wanted to delete the dictionary with a name value of "Bill" how would I go about doing it?
people.delete_if{|element| element['name'] == 'Bill'}
# => [{"name"=>"Jim", "car"=>"Porche", "houseSize"=>"big"}]
BTW: Your hashes are missing some commas.
people = [
{
'name' => "Jim",
'car' => "Porche",
'houseSize' => "big"
},
{
'name' => "Bill",
'car' => "Honda",
'houseSize' => "small"
}
]
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!