0

How to iterate over an array with hash that has the position included? Could I remove it? I've never come across this before.

var data = [
 {
   id: 1,
   foo: "hello",
   ...,
 },
 {
   id: 2,
   foo: "world",
   ...,
 }
]

When I POST this data to my controller, in my console I see the data params also includes the position (0, 1, 2, etc) so I cant iterate over it properly. The full look would be:

React:

...

$.ajax({
  url: '/the-url',
  type: 'POST',
  data: {'line_items': data, // this would have been this.state.data
          ...
        },
  success: function (data, xhr) {
      console.log("success")

  }.bind(this),
  error: function (data) {
      console.log("error")
  }
});

...

Controller:

...

line_items = params[:line_items]
line_items.map do |l| l.id end # error

...

Console:

0
{"id"=>"1", "foo"=>"hello"},
1
{"id"=>"2", "foo"=>"world"},

Also Rails 5 says map is depreciated. So what to use?

Sylar
  • 11,422
  • 25
  • 93
  • 166

0 Answers0