0

I am trying to convert this object to json string but the result is not returning the object for address.

here's the code

{
        name: $('[name="name"]').val(),
        age: $('[name="age"]').val(),
        sex: $('[name="sex"]').val(),
        birthday: $('[name="birthday"]').val(),
        about: $('[name="about"]').val(),
        addresses: mapAlfredObject(getAlfredObject('addresses')),
        contacts: mapAlfredObject(getAlfredObject('contacts')),
    };

here's how I implemented the mapAlfredObject method

function mapAlfredObject(obj) {
    var items = [];
    for (var index in obj) {
        items[obj[index]['name']] = {
            value: obj[index]['value'],
            type: obj[index]['type'],
            comment: obj[index]['comment']
        };
    }
    return items;
}

here's the log from console

enter image description here

and the output from JSON.stringify

{"name":"","age":"","sex":"","birthday":"any","about":"","addresses":[],"contacts":[]}
5
  • please add the object in text form. Commented Jun 9, 2016 at 7:48
  • can you reproduce the problem in snippet or fiddle? Commented Jun 9, 2016 at 7:50
  • 2
    var items = []; or var items = {}; Commented Jun 9, 2016 at 7:56
  • 1
    @PranavCBalan it worked by using var items = {}; thanks! Commented Jun 9, 2016 at 7:59
  • Could't you use the jQuery method .serializeArray ? Commented Jun 9, 2016 at 7:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.