Merge two arrays that both have key value pairs (Ruby)

Mark Ortega

I am wondering how to merge these two arrays into one clean array in Ruby
Both arrays share one similar key:value pair. I am trying to merge information from these two separate arrays that have information for the same person. One array has his name. The other array has his job and age. Both arrays have an id matching to the same person.
An example of what I am trying to do

array1 = [ {:id => 1, :name => "Bob"}, {:id => 2, :name => "Tim"}]
array2 = [ {:id => 1, :job => "firefighter", :age => 25}, { :id => 2, :job => "accountant",      :age => 30} ]


new_array = [ {:id=> 1, name => "Bob", :job => "firefighter", :age => 25}, { :id => 2, :name => "Tim", :job => "accountant", :age => 30} ]
August

You could do something like this:

new_array = array1.each_with_index.map { |x, i| x.merge array2[i] }
# => [{:id=>1, :name=>"Bob", :job=>"firefighter", :age=>25}, {:id=>2, :name=>"Tim", :job=>"accountant", :age=>30}]

If you want a solution that is not dependent on the order of the array, and instead uses the :id to match the hashes:

array1.map { |x| x.merge (array2.find { |h| h[:id] == x[:id] } || {}) }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to merge the key-value pairs of two json arrays? - Javascript

Given two arrays of hashes, How can we match hashes from both arrays that have a matching key:value

Merge two pairs in the same array if the pairs have the same value

Merge two arrays in Ruby by unique key

Merge two arrays of both values and make as one array like as one key other value in php

How to merge hashes with different key/value pairs in array of hashes? Ruby

Why do some arrays seem to have key value pairs

Merge objects have array, and keep both value of arrays

Two array merge to form new array of key value pairs in javascript

How can I merge two dictionaries with multiple key value pairs

In ruby how can we merge two given hash into single hash and replace duplicate key value with greater of both the value

Comparing Two Arrays that don't have standard keys in the object key/value pairs, pushing missing values to first array in javascript

How to replace key/value pairs in an object of two arrays of objects

Merging two or more dictionaries when they have the same key value pairs

Ruby - Merge two arrays and remove values that have duplicate

Merge two arrays of objects but have the second array pushed into an object key

Merge nested maps with similar outer keys, keeping both inner map's key value pairs

Merge two arrays by value

Merge Two Arrays to Have an Integer Value be an near equal as possible

Create or Update key value pairs in both maps

Merge sum two numpy arrays, one as key, another as value

How to merge two multi dimensional arrays by key value in php?

Merge two arrays of objects but with uniqueness on a certain key/value

Merge two arrays of objects based on nested object key and value

Ruby merge two arrays of hashes

How to merge 2 arrays of equal length into a single dictionary with key:value pairs in Godot?

How to merge two arrays if they both don't have the same size DART?

How to create an object with two key/value pairs from two separate arrays

Merge array with varying key value pairs

TOP Ranking

HotTag

Archive