1

i have a set of inputFields looking like :

<g:textField name="adrstrasse" data-id="adressen[${i}].strasse" class="newaddr" id="daten_vornametarea" value="${adresse.strasse}"/>
<g:textField name="adrort" data-id="adressen[${i}].ort" class="newaddr" id="daten_vornametarea" value="${adresse.telefon}"/>
<g:textField name="adrtelefon" data-id="adressen[${i}].telefon" class="newaddr" id="daten_vornametarea" value="${adresse.telefon}"/>

and i want to return those as a stringified map,

doing so :

var addrmap = $('.newaddr').map(function() {
        var $item = $(this);
                return {
        name: $item.data('id'), 
        value: $item.val()
    };
    }).get();
    var neu = JSON.stringify(addrmap); 
    alert(neu);

I´m getting a map looking like :

[{"name":"adressen[0].strasse","value":"Hanswarft1"},{"name":"adressen[0].ort","value":"Hallig Hooge"},{"name":"adressen[0].telefon","value":"12345678"}]

But I want it to look like :

[{"adressen[0].strasse":"Hanswarft1"},{"adressen[0].ort":"Hallig Hooge"},{"adressen[0].telefon":"12345678"}]

When I try something like this, ofcourse i get syntax errors

    var addrmap = $('.newaddr').map(function() {
        var $item = $(this);
                return {
        $item.data('id'):
        $item.val(),
    };
    }).get();
    var neu = JSON.stringify(addrmap); 
    alert(neu);

How do I return the map with data-id as parameter and value as value ? thanks in advance

3
  • Your last update completely altered the question Commented Feb 2, 2013 at 10:51
  • yes it did, ah i have to rethink it anyway, so dint try to answer this, igor dymov gave the perfect answer Commented Feb 2, 2013 at 10:57
  • 1
    I rollbacked the question. Otherwise, question and answer won't fit together Commented Feb 2, 2013 at 11:00

1 Answer 1

2

That might do the job:

var addrmap = $('.newaddr').map(function () {
    var $item = $(this);
    var obj = {};

    obj[$item.data('id')] = $item.val();
    return obj;
}).get();
Sign up to request clarification or add additional context in comments.

1 Comment

yeah thats the totally correct answer to my question, thanks alot,this made me, unfortunetly, recognize i need the map to look even more difficult i updated my question

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.