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 7995961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:34:34+00:00 2026-06-04T14:34:34+00:00

I have a JSON file with parsed data stored in a @colors instance variable,

  • 0

I have a JSON file with parsed data stored in a @colors instance variable, as follows:

[{:color=>"red", :value=>"#f00"} {:color=>"green", :value=>"#0f0"} {:color=>"blue", :value=>"#00f"} {:color=>"cyan", :value=>"#0ff"} {:color=>"magenta", :value=>"#f0f"} {:color=>"yellow", :value=>"#ff0"} {:color=>"black", :value=>"#000"}]

Now I want to iterate through this output to create a table in a view where there is

<tr><td>color</td><td>value</td></tr>

When I derive another instance variable like this —

@even_colors = @colors.values_at(* @colors.each_index.select {|i| i.even?}).map(&:values)

I get an array of arrays consisting of every other color/value pair

[["red", "#f00"], ["blue", "#00f"], ["magenta", "#f0f"], ["black", "#000"]]

But what I want to create two separate arrays, one consisting only of the color names indicated by :color (red, blue, etc.) and the other consisting of just the hexs indicated by :value (#f00, #00f, etc.). I can’t seem to figure out how to do that. Anyone have any suggestions? Thanks …

  • 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-04T14:34:35+00:00Added an answer on June 4, 2026 at 2:34 pm

    You could do it with two passes through @colors:

    names = @colors.map { |h| h[:color] }
    hexes = @colors.map { |h| h[:value] }
    

    Or you could it with on pass:

    parts = @colors.each_with_object({ :names => [ ], :hexes => [ ]}) do |h, parts|
        parts[:names].push(h[:color])
        parts[:hexes].push(h[:value])
    end
    

    then look at parts[:names] for the color names and parts[:hexes] for the hex values.

    I don’t really see why you want to split @colors up though, you could produce your table straight from@colors:

    <table>
        <% @colors.each do |h| %>
            <tr><td><%= h[:color] %></td><td><%= h[:value] %></td></tr>
        <% end %>
    </table>
    

    Breaking @colors into two arrays seems like busy-work to me.

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

Sidebar

Related Questions

I have a flash file which loads data from JSON and parses it. It
I have a json file of data and the only item for time i
I have a JSON file that has more name value pairs than I care
hello all i have a json file i.e { data: [ { name: The
I have a set of offline data in JSON file which i kept in
I have parse json file which contain more than hundred location name, there latitude
I have a huge JSON file to parse in my Android app. I suppose
I have a large JSON file that contains A LOT of similar code. It's
I have the following json code file named: sections.json { section1: { priority: 1,
I have a JSON string (external file) which has an element which can either

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.