Ruby map specific hash keys to new one - ruby

I've got an array full of hashes of which I want to combine specific keys to a new one, e.g.
[{ firstname: 'john', lastname: 'doe', something: 'else', key: ... }, { firstname: 'Joe', lastname: 'something', something: 'bla', key:... }]
should become
[{ name: 'john doe' },{ name: 'Joe something' }]
Please note: there are more keys in the hash as first and lastname. Is there a common ruby method to do this? Thanks!

Just do as
array = [{ firstname: 'john', lastname: 'doe' }, { firstname: 'Joe', lastname: 'something' }]
array.map { |h| { :name => h.values_at(:firstname, :lastname) * " " } }
# => [{:name=>"john doe"}, {:name=>"Joe something"}]
Read this Hash#values_at and Array#* .

This is:
a = [{ firstname: 'john', lastname: 'doe' }, { firstname: 'Joe', lastname: 'something' }]
a.map { |n| { name: n.values.join(' ') } }
# => [{:name=>"john doe"}, {:name=>"Joe something"}]

Related

How do I remove duplicate items from observable array based on multiple properties?

If I have an array of items like
[
{id: 1, name: 'Sam', gender: 'boy'},
{id: 2, name: 'Mary', gender: 'girl'},
{id: 3, name: 'Sam', gender: 'boy'}
]
Matching on just name and gender, how do I reduce it to the following result?
[
{id: 1, name: 'Sam', type: 'boy'},
{id: 2, name: 'Mary', type: 'girl'}
]
Let try
items$.pipe(map(this.uniqueArray))
uniqueArray(array: any[]): any[] {
return array.filter(
(item, index, self) =>
index === self.findIndex((x) => x.name === item.name)
);
}
https://stackblitz.com/edit/angular-isqjpa?file=src/app/hello.component.ts

How to extract from an array of hash

people = [
{id: 101, first_name: 'John', last_name: 'Doe'},
{id: 102, first_name: 'Tom', last_name: 'Rogers'},
{id: 103, first_name: 'Bill', last_name: ''}
]
I want to put a list of name like this
"John Doe, Tom Rogers, Bill"
How can i achieve this is in ruby?
Input
people = [
{ id: 101, first_name: 'John', last_name: 'Doe' },
{ id: 102, first_name: 'Tom', last_name: 'Rogers' },
{ id: 103, first_name: 'Bill', last_name: '' }
]
Code
p people.map { |x| x[:first_name].to_s + ' ' + x[:last_name].to_s }
.map(&:strip)
.join(",")
Output
"John Doe,Tom Rogers,Bill"
The other answers are good, but I wanted to add a demonstration of string interpolation.
people
.map { |p| "#{p[:first_name]} #{p[:last_name]}" }
.join(", ")
#rajagopalan has a good idea in using strip to remove unwanted whitespace at the beginning and end of the first and last name combination, but it might be even better to run this on each component of the name.
people
.map { |p| "#{p[:first_name].strip} #{p[:last_name].strip}" }
.join(", ")
I would do
people
.map { |p| p.values_at(:first_name, :last_name).join(' ').strip }
.join(', ')
#=> "John Doe, Tom Rogers, Bill"
or
people
.map { |p| "#{p[:first_name]} #{p[:last_name]}".strip }
.join(', ')
#=> "John Doe, Tom Rogers, Bill"

How to merge array of hashes based on hash value but not merge values instead override

I have an array of hashes like this:
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
And this is second group array of hashes:
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
What I want is, merge these two arrays into one, merge based on :email and keep latest (or first) :name.
Expected Result (latest name overrided):
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
or (first name preserved)
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
So, basically, I want to group by :email, retain one :name, drop dupe emails.
The examples found on SO is creates an array of values for :name.
Ruby 2.6.3
Maybe you could just call Array#uniq with a block on email key of the concatenation (Array#+) of the two arrays:
(ary1 + ary2).uniq { |h| h[:email] }
a1 = [
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
a2 = [
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
Let's first keep the last:
(a1+a2).each_with_object({}) { |g,h| h.update(g[:email]=>g) }.values
#=> [{:name=>"AnotherNameSameEmail", :email=>"c#f.com"},
# {:name=>"JohnAnotherName", :email=>"j#g.com"},
# {:name=>"Clark", :email=>"x#z.com"},
# {:name=>"Mark", :email=>"d#o.com"}]
To keep the first, do the same with (a1+a2) replaced with (a2+a1), to obtain:
#=> [{:name=>"Pratha", :email=>"c#f.com"},
# {:name=>"John", :email=>"j#g.com"},
# {:name=>"Mark", :email=>"d#o.com"},
# {:name=>"Clark", :email=>"x#z.com"}]

defaultValue not woking on CheckboxGroupInput

I'm trying to write a simple check box group like this:
<CheckboxGroupInput source="test" defaultValue={{ _id: 123 }} choices={[
{ _id: 123, name: 'Leo Tolstoi', sex: 'M' },
{ _id: 456, name: 'Jane Austen', sex: 'F' },
]} />
I couldnt find an example of how to specify the defaultValue for CheckboxGroupInput.
i also tried passing:
defaultValue={{ _id: 123, name: 'Leo Tolstoi', sex: 'M', checked: true }}
defaultValue={[{ _id: 123, name: 'Leo Tolstoi', sex: 'M', checked: true }]}
defaultValue="123"
any leads are appreciated!
Don't forget to set the optionValue prop to _id. The default is id. See the documentation: https://marmelab.com/admin-on-rest/Inputs.html#checkboxgroupinput

What is this format in ruby?

In a book they showed me this declaration:
friends = [ { first_name: "Emily", last_name: "Laskin" }, { first_name: "Nick", last_name: "Mauro" }, { first_name: "Mark", last_name: "Maxwell" } ]
This doesn't look like a hash. And when I enter it in IRB i get an error.
What is this format?
It's an array of hashes, written in the Ruby 1.9 hash syntax.
{ first_name: "Emily", last_name: "Laskin" }
is equivalent to:
{ :first_name => "Emily", :last_name => "Laskin" }
The {key: value} syntax is new in 1.9 and is equivalent to {:key => value}.
It is an array of hashes, only the hashes are 1.9 style hashes.

Resources