1

I have:

a =  [{"_id"=>BSON::ObjectId('569a58b8e301d083c300000c')}]

and I want it to be:

[BSON::ObjectId('569a58bee301d083c3000752')]

I was experimenting with

a.map{|e| e.map{|k, v| v }}

but it gives me nested array:

[[BSON::ObjectId('569a58b8e301d083c300000c')]]

I'll appreciate any help.

3 Answers 3

3

If you just had a hash:

h = {"_id"=>BSON::ObjectId('569a58b8e301d083c300000c')}

and you wanted to fetch the BSON::ObectId, you would call Hash#[]:

h["_id"] #=> BSON::ObjectId('569a58b8e301d083c300000c')

The same works with map:

a.map { |h| h["_id"] }
#=> [BSON::ObjectId('569a58b8e301d083c300000c')]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, stupid of me not to think about that. Have a nice day!
Not just [a.first["_id"]]?
0

A cleaner solution.

a = [{"id"=>"1234"},{"id"=>"9876"}]
a.flat_map(&:values)
=> ["1234", "9876"]

Comments

0

I'd use values, then call flatten.

a.collect(:&values).flatten

Or, if there'll only ever be one value

a[0].values[0]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.