0

I have two arrays, the first array contains field name, type and id.

arr1 = [
    {
        "n" => "cat",
        "t" => 0,
        "id" => "42WTd5"
    },
    {
        "n" => "dog",
        "t" => 0,
        "id" => "1tM5T0"
    }
]

Second array contains field, id and value.

arr2 = [
    {
        "42WTd5"=>"meow",
        "1tM5T0"=>"woof"
    }
]

How can I join them by id to produce the following result.

cat: meow
dog: woof

Any help is appreciated.

4
  • What do you mean by produce? Commented Sep 5, 2017 at 18:33
  • 1
    what is that result? Is it an Array of String values (["cat: meow","dog: woof"]) or is it a Hash ({cat: "meow", dog: "woof"}) or is it just output or a single String with a new line break or a rabbit in a hat with a bat? (Please explain) Commented Sep 5, 2017 at 18:43
  • What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a minimal reproducible example. Stack Overflow is not a "write-my-code-for-me-service"! If you are looking for that, hire a programmer. Commented Sep 6, 2017 at 6:47
  • "How can I join them by id to produce the following result." – You do it by writing a program which does that. If you have a problem with your program, carefully read the documentation of all the methods, classes, modules, and libraries you are using, write tests for your programs, trace the execution with pen and paper, single-step it in a debugger, then sleep on it, start again from the beginning, sleep on it again, and then and only then ask a focused, narrow question on Stack Overflow Commented Sep 6, 2017 at 6:48

3 Answers 3

1

I think you want your result to be a Hash, in which case this would do the job:

def match_animals_to_sounds(animal_array, sound_array)
  sounds = sound_array.first
  animal_array.map { |animal| [animal['n'], sounds[animal['id']]] }.to_h
end

>> match_animals_to_sounds(arr1, arr2)
=> {"cat"=>"meow", "dog"=>"woof"}

Your arr2 is unusual in that it is an Array of a single element. I'm just calling #first on it to pull out the Hash inside. If you expect some version of this Array to have more than one element in the future, you'll need to rethink the first line of this method.

The second line is standard Ruby Array manipulation. The first part maps each animal to a new Array of two-element Arrays containing each animal's name and sound. At the end, #to_h converts this array of two-element arrays to a single Hash, which is much more useful than an array of strings. I don't know what you intended in your question, but this is probably what you want.

If you prefer to work with Symbols, you can change the second line of the method to:

animal_array.map { |animal| [animal['n'].to_sym, sounds[animal['id']].to_sym] }.to_h

In which case you will get:

>> match_animals_to_sounds(arr1, arr2)
=> {:cat=>:meow, :dog=>:woof}
Sign up to request clarification or add additional context in comments.

Comments

0

This is a way to do it.

sounds = arr2[0]
results = arr1.map do |animal|
  "#{animal["n"]}: #{sounds[animal["id"]]}"
end
puts results
# => cat: meow
# => dog: woof

Seems like the second array should just be a hash instead. There's no point creating an array if there's only one element in it and that number won't change.

pointless one-liner (don't use this)

puts arr1.map { |x| "#{x["n"]}: #{arr2[0][x["id"]]}" }

Comments

0

You can also get the join result by following code

arr1.collect{ |a| {a["n"] => arr2[0][a["id"]]} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.