1

I'm passing an array of arrays from jQuery to Rails through Ajax:

search_array = [arr1, arr2];

In the console, it shows it to me as this is being passed:

search_array => {"0" => ["Trader"], "1" => ["x1"]}

Which is correct.

How do I access the values of each array? I am able to access just arrays of values through normal declaration:

myArrayInRails = params[:searchArray]

and then do normal calls such as:

myVar = myArrayInRails[0] 

And get the value, but I can't access deeper in with:

myVar = myArrayInRails[0][0]

It gives me:

Undefined method '[]' for nil:nilClass.

1 Answer 1

1

What you have there is a hash so you shouldn't be accessing it through indexes. Doing it through keys will work, p.e.:

params[:search_array]['1'] 

Or

params[:search_array][:1]
Sign up to request clarification or add additional context in comments.

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.