0

I made a table component using react js, which uses columns to display data (which works fine with other data). For example, column 1 would show the title, column 2 the year, and column 3 the format.

Here is an example of my JSON:

{"movies": [{"title": "Iron Man", "year": "2008", "format": "DVD"}, {"title": "Iron Man 2", "year": "2010", "format": "DVD"}, {"title": "Iron Man 3", "year": "2013", "format": "DVD"}]}

Here is my code to populate the table, but it does not seem to work:

@movieList = #Makes a call to my mock API to get list of movies
@movies = Array.new

@movieList.each do |item|
 @movie = Hash.new
 @movie[:column1] = item[:title]
 @movie[:column2] = item[:year]
 @movie[:column3] = item[:format]
 @movies << @movie
end

I need some advice to overcome a "no implicit conversion of symbol into integer error" I get. Could anyone offer some advice and point out where I am going wrong?

3 Answers 3

2

tl;dr

use @movieList["movies"].each

explanation

The issue here, is that you act as though your @movieList is ann array, when it is actually a hash (assuming @movieList is the JSON you showed).

each works on both arrays and hashes. However, when you use it on a hash, the block is passed |key, val|. Also, assigning block variables is optional. So, when you say @movieList.each do |item|, item is actually the top level key of the hash ("movies").

Strings such as "movies" respond to [] indexing with numbers. That's why you get the error no implicit conversion of symbol into integer ... because you pass a symbol to String#[] and it expects an integer.

Another way to write this code, that is more idiomatic, would be like so:

@movies = @movieList["movies"].map do |movie|
  {
    column1: movie["title"],
    column2: movie["year"],
    column3: movie["format"]
  }
end
Sign up to request clarification or add additional context in comments.

Comments

0

try reassigning

@movieList = @movieList[:movies] this will solve your problem. You're trying to iterate a object instead of an array.

lemme know if it solves your problem.

Comments

0

You need to loop movies using @movieList["movies"] as your JSON is a hash that has a key 'movies' and an array of movies as a value => {'movies': [{...},{...},...]}

As @max pleaner explained assigning block variables is optional, but when you use each on a hash(your JSON in this case) and provide only one block variable (instead of two refering to the keys and values of the hash), your key-value pairs are converted to two-element arrays inside the block where first element is the key and second one is the value of the pair.

Your item looks like this inside your each block - ['movies', [{movie1}, {movie2},..]], hence:

item[0] # 'movies'
item[1] # [{movie1}, {movie2},...]

As arrays expect indexing with integers and you supply symbol (item[:title]), you receive:

TypeError (no implicit conversion of Symbol into Integer)

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.