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

  • Home
  • SEARCH
  • 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 829795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:56:23+00:00 2026-05-15T03:56:23+00:00

I have two arrays, each holding arrays with attribute hashes. Array1 => [[{attribute_1 =

  • 0

I have two arrays, each holding arrays with attribute hashes.

Array1 => [[{attribute_1 = A}, {attribute_2 = B}], [{attribute_1 = A}, {attribute_4 = B}]]
Array2 => [{attribute_3 = C}, {attribute_2 = D}], [{attribute_3 = C, attribute_4 = D}]]

Each array in the array is holding attribute hashes for an object. In the above example, there are two objects that I’m working with. There are two attributes in each array for each of the two objects.

How do I merge the two arrays? I am trying to get a single array of ‘object’ arrays (there is no way to get a single array from the start because I have to make two different API calls to get these attributes).

DesiredArray => [[{attribute_1 = A, attribute_2 = B, attribute_3 = C, attribute_4 = D}],
                 [{attribute_1 = A, attribute_2 = B, attribute_3 = C, attribute_4 = D}]]

I’ve tried a couple things, including the iteration methods and the merge method, but I’ve been unable to get the array I need.

  • 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-05-15T03:56:23+00:00Added an answer on May 15, 2026 at 3:56 am

    You seem to have parallel arrays of hashes. We can use zip to turn the parallel arrays into a single array of arrays of hashes. We can then map each array of hashes into a single hash using inject and merge:

    #!/usr/bin/ruby1.8
    
    require 'pp'
    
    array1 = [{:attribute_1 => :A, :attribute_2 => :B}, {:attribute_1 => :A, :attribute_4 => :B}]
    array2 = [{:attribute_3 => :C, :attribute_2 => :D}, {:attribute_3 => :C, :attribute_4 => :D}]
    
    pp array1.zip(array2).collect { |array| array.inject(&:merge) }
    # => [{:attribute_2=>:D, :attribute_1=>:A, :attribute_3=>:C},
    # =>  {:attribute_4=>:D, :attribute_1=>:A, :attribute_3=>:C}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays each array has some values for instance: int a[] =
I have two arrays. In each array I have objects with lots of properties
I have two arrays, each containing strings. The first array is a list of
We have two unsorted arrays and each array has a length of n. These
I have two arrays: $array1: Array ( [0] => 2032 [1] => 2027 [2]
I have two arrays: 1. each object here is a row retrived from database.
I have a two dimensional JSON array where each element contains several attributes. The
If I have an array of doubles that each have EXACTLY two decimal places,
I have two arrays... $Name = array(a, b, c, d); $Value = array(1, 2,
I have two one dimensionaly arrays - each of the same length; temperature and

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.