2

I'm looping inputs in a table, that don't have any form tag. I get the values correctly. I want to build with their values an object that contains multiple objects.

What i'm expecting?

alarms = { alarm: { status_id: '1', alarm_name: 'Critic', user_id_created: '30021061' }, alarm: { status_id: '1', alarm_name: 'Middle', user_id_created: '30021061' }, alarm: { status_id: '1', alarm_name: 'Bottom', user_id_created: '30021061' }, ... };

What i'm getting? The last object in the loop.

alarms = { alarm: { status_id: '1', alarm_name: 'Bottom', user_id_created: '30021061' } };

Here is the code:

var alarms = {}
$('.new_alarm').each(function() {
    var status_id = $(this).children('.status').children().val(),
        alarm_name = $(this).children('.data').children('input[name="alarm_name"]').val(),
        user_id = $('#user_id').text();
        objAux = {};

    if(alarm_name) {            
        objAux = {
            alarm: {
                'status_id': status_id,
                'alarm_name': alarm_name,
                'user_id_created': user_id
            }
        };
    }

    alarms = $.extend(true, alarms, objAux);          
});

What's wrong with the jQuery extend method? Why is not merging the objects?

1
  • Did you really have to post that JSON all on one line? Commented Jun 23, 2011 at 20:24

2 Answers 2

4

If I'm not mistaken, what you want is actually impossible. It's akin to saying you want an array to have 5 values for the a[1].

You could implement this using an array instead of an object:

alarms = [{...},{...},{...}];

What you're writing is actually this:

alarms['alarm'] = {...};
alarms['alarm'] = {...};
alarms['alarm'] = {...};
alarms['alarm'] = {...};
Sign up to request clarification or add additional context in comments.

3 Comments

I need it that way to send it over AJAX to a Rails server. Rails only process, if I'm not mistaken, only hashes (aka JSON objects).
JSON objects can definitely contain arrays and Rails should have no problem with that.
Ruby does not mind JSON arrays. Not sure where you got that from, betacar.
1

You're overriding the same property "alarm" on every iteration.

You should be creating an Array and then push() the values on the end of an Array.

var alarms = [
      {name: 'alarm1'}
    , {name: 'alarm2'}
    , {name: 'alarm3'}
];


var new_alarms = [];


$(alarms).each(function() {
    console.log(this);
    new_alarms.push(this);
});


console.log(alarms, new_alarms);

See: http://jsfiddle.net/y22Hk/

3 Comments

I need it that way to send it over AJAX to a Rails server. Rails only process, if I'm not mistaken, only hashes (aka JSON objects).
Ruby does not mind JSON arrays. Not sure where you got that from, betacar. Also, "I need it that way" is an odd thing to say about something that is physically and mathematically impossible...
A read it while ago (don't remember where). Thank's for the article. :)

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.