0

I'm not looking native javascript only. I'm open to jQuery and underscore.js as well. I've googled a bit and I found questions/answers only explaining how to combine two objects and not more.

There is .extend from jquery which can do that. I'm looking for something that combines n objects with different properties into single object.

I'm actually doing a $('form').serializeArray() and then I get array of objects from the form. Is there a way I can get a single object from a form? Or how to combine more objects with different properties into single object?

3
  • You have an array. $.extend is for objects. What exactly are you trying to do? submit multiple forms through 1 ajax request? Commented Dec 11, 2013 at 19:41
  • Can you give an example of the objects you're working with, and the expected output for those example objects? Commented Dec 11, 2013 at 19:42
  • he has [{name: "inputname",value:"inputvalue"},...] Commented Dec 11, 2013 at 19:43

1 Answer 1

1

To convert a form to a json object use this. quote from answer above:

serializeArray already does exactly that, you just need to massage the data into your required format:

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

To combine multiple objects use underscore extend:

_.extend({name: 'moe'}, {age: 50}, {test: "test"});

or jQuery extend:

$.extend({name: 'moe'}, {age: 50}, {test: "test"})
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.