最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Extend a JavaScript object - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Feb 28, 2018 at 21:32 str 45.1k18 gold badges114 silver badges134 bronze badges asked Jun 23, 2011 at 19:49 betacarbetacar 4262 gold badges9 silver badges21 bronze badges 1
  • Did you really have to post that JSON all on one line? – Lightness Races in Orbit Commented Jun 23, 2011 at 20:24
Add a ment  | 

2 Answers 2

Reset to default 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'] = {...};

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/y22Hk/

发布评论

评论列表(0)

  1. 暂无评论