Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:28:32+00:00 2026-06-05T17:28:32+00:00

I have an simple array array = [apple, orange, lemon] array2 = [[apple, good

  • 0

I have an simple array

array = ["apple", "orange", "lemon"] 

array2 = [["apple", "good taste", "red"], ["orange", "bad taste", "orange"], ["lemon" , "no taste", "yellow"]]

how can i convert in to this hash whenever element in array match the first element of each element in array2?

hash = {"apple" => ["apple" ,"good taste", "red"],
        "orange" => ["orange", "bad taste", "orange"], 
        "lemon" => ["lemon" , "no taste", "yellow"] }

I am quite new to ruby, and spend a lot to do this manipulation, but no luck, any help ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:28:34+00:00Added an answer on June 5, 2026 at 5:28 pm

    If the order of the mapping between the key and pairs should be based on the first element in array2, then you don’t need array at all:

    array2 = [
      ["apple", "good taste", "red"],
      ["lemon" , "no taste", "yellow"],
      ["orange", "bad taste", "orange"]
    ]
    
    map = Hash[ array2.map{ |a| [a.first,a] } ]
    p map
    #=> {
    #=>   "apple"=>["apple", "good taste", "red"],
    #=>   "lemon"=>["lemon", "no taste", "yellow"],
    #=>   "orange"=>["orange", "bad taste", "orange"]
    #=> }
    

    If you want to use array to select a subset of elements, then you can do this:

    # Use the map created above to find values efficiently
    array = %w[orange lemon]
    hash  = Hash[ array.map{ |val| [val,map[val]] if map.key?(val) }.compact ]
    p hash
    #=> {
    #=>   "orange"=>["orange", "bad taste", "orange"],
    #=>   "lemon"=>["lemon", "no taste", "yellow"]
    #=> }
    

    The code if map.key?(val) and compact ensures that there is not a problem if array asks for keys that are not present in array2, and does so in O(n) time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a simple PHP loop like this one // Bad example $array
I have a simple simulated array with two elements: bowl["fruit"] = "apple"; bowl["nuts"] =
I have an array like this: String n[] = {google,microsoft,apple}; What I want to
I have a simple array of stuff: $array = array(apples,oranges,strawberries); I am trying to
I have a simple array with bank holidays: var bank_holidays = ['06/04/2012','09/04/2012','07/05/2012','04/06/2012','05/06/2012','27/08/2012','25/12/2012','26/12/2012','01/01/2013','29/03/2013','01/04/2013','06/05/2013','27/05/2013']; I want
I have a simple array with x number of items. I am displaying them
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have a simple associative array: $ar = array( 1=>'foo', 2=>'bar', 5=>'foobar', 8=>'barfoo' )
I have a very simple array (please focus on the object with "points.bean.pointsBase" as
I have a simple 2D array: int[,] m = { {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} }; How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.