1

I have this piece of JS, where I have create action with Ajax:

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
      table: "#user_groups_table",
      template: '#user_groups_form',
      display: "details",
      idSrc: "id",
      ajax: {
          create: {
              type: 'POST',
              url:  '/strongbolt/user_groups',
          }
      },
      fields: [ {
              name: "name"
          }, {
              name: "description"
          }, {
              type: "checkbox",
              name: "users[].id",
              optionsPair: {
                label: 'name',
                value: 'id'
              }
            }, {
              type: "checkbox",
              name: "roles[].id",
              optionsPair: {
                label: 'name',
                value: 'id'
              }
            }
      ]
    } );

    editor.on( 'preSubmit', function ( e, data, action ) {
      if ( action === 'create' ) {
        data.strongbolt_user_group = {
            "name": data.data[0].name,
            "description": data.data[0].description,
            "user_ids": data.data[0].users,
            "role_ids": data.data[0].roles
        };
        delete data.data;
      }
    } );

The last section which starts with editor.on( 'preSubmit', function ( e, data, action ) { basically modifies data before they are passed to server.

At the moment I get my params in terminal like this:

{
  "strongbolt_user_group"=>{
    "name"=>"Some test group",
    "description"=>"Some test description",
    "user_ids"=>{"0"=>{"id"=>"3"}, "1"=>{"id"=>"2"}, "2"=>{"id"=>"5"}}, 
    "role_ids"=>{"0"=>{"id"=>"1"}, "1"=>{"id"=>"2"}}
  }
}

however I need it to be like this:

{
  "strongbolt_user_group"=>{
    "name"=>"Some test group",
    "description"=>"Some test description",
    "user_ids"=>["3", "2", "5"], 
    "role_ids"=>["1", "2"]
  }
}

Basically I need user_ids and role_ids to be array. How do I modify this in my JS, please? Thank you!

6
  • "user_ids"=>{"0"=>{"id"=>"3"}, "1"=>{"id"=>"2"}, "2"=>{"id"=>"5"}} from where u getting this data? Its not JavaScript array or object .Look like PHP Commented Jun 28, 2017 at 4:25
  • @RIYAJKHAN It's params passed from JS form input - not PHP. I use Rails and that's why I need to modify my params to array. Commented Jun 28, 2017 at 4:28
  • So,=> part coming due to RAILS? Commented Jun 28, 2017 at 4:29
  • @RIYAJKHAN Probably... basically this is is what I see in my terminal. Main thing is I need those user_ids and role_ids to be array like in my example. Commented Jun 28, 2017 at 4:31
  • its better if you do this in rail context and not in JS.Its rails associate array styntax.Can please try to console the same object in Browser console? and then share the result Commented Jun 28, 2017 at 4:35

1 Answer 1

1

You have to map the array (or array-like object) of objects to an array of their ids:

"user_ids": Array.prototype.map.call(data.data[0].users, function(o) { return o.id; }),

If you are certain that data.data[0].users is an array, then just use map without call like:

"user_ids": data.data[0].users.map(function(o) { return o.id; }),

or even shorter in ES 6's arrow functions:

"user_ids": data.data[0].users.map(o => o.id),

Note: same applies for role_ids.

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.