string to indexed property











up vote
1
down vote

favorite












I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]. Using $(form).serializeArray() I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.



const seializedForm = $(event.currentTarget.form).serializeArray();

const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});


The problem here is that the result isn't exactly what I need:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}


I'd like it to be:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}


Any suggestions?










share|improve this question






















  • Can you add the form?
    – ksav
    Nov 10 at 21:18















up vote
1
down vote

favorite












I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]. Using $(form).serializeArray() I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.



const seializedForm = $(event.currentTarget.form).serializeArray();

const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});


The problem here is that the result isn't exactly what I need:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}


I'd like it to be:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}


Any suggestions?










share|improve this question






















  • Can you add the form?
    – ksav
    Nov 10 at 21:18













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]. Using $(form).serializeArray() I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.



const seializedForm = $(event.currentTarget.form).serializeArray();

const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});


The problem here is that the result isn't exactly what I need:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}


I'd like it to be:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}


Any suggestions?










share|improve this question













I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]. Using $(form).serializeArray() I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.



const seializedForm = $(event.currentTarget.form).serializeArray();

const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});


The problem here is that the result isn't exactly what I need:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}


I'd like it to be:



{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}


Any suggestions?







javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 21:13









Sylvain Girard

9918




9918












  • Can you add the form?
    – ksav
    Nov 10 at 21:18


















  • Can you add the form?
    – ksav
    Nov 10 at 21:18
















Can you add the form?
– ksav
Nov 10 at 21:18




Can you add the form?
– ksav
Nov 10 at 21:18












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:






const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];

const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';

return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});

console.log(gdpr);








share|improve this answer























  • @customcommander - Yep. It should always be an array, even if it has one item. Fixed.
    – Ori Drori
    Nov 11 at 5:11










  • I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
    – Sylvain Girard
    Nov 11 at 6:18


















up vote
0
down vote













Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string other, then we can assume that it is part of the other form collection structure?



const seializedForm = $(event.currentTarget.form).serializeArray();

const gdpr = seializedForm.reduce((aggragation, option) => {
if (isInArrayOfOptions(option)) {
return {
...aggragation,
/* Return a new array combining the current with the next option.value*/
'other': [...aggragation.other, ...[option.value === 'true']]
}
}
return {
...aggragation,
[option.name]: option.value === 'true'
}
}, {});





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243464%2fstring-to-indexed-property%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:






    const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];

    const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
    const isArray = name.includes('[');
    const key = name.replace(/[.+]/g, '');
    const val = value === 'true';

    return {
    ...aggragation,
    [key]: isArray ? [...aggragation[key] || , val] : val
    };
    }, {});

    console.log(gdpr);








    share|improve this answer























    • @customcommander - Yep. It should always be an array, even if it has one item. Fixed.
      – Ori Drori
      Nov 11 at 5:11










    • I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
      – Sylvain Girard
      Nov 11 at 6:18















    up vote
    2
    down vote



    accepted










    For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:






    const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];

    const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
    const isArray = name.includes('[');
    const key = name.replace(/[.+]/g, '');
    const val = value === 'true';

    return {
    ...aggragation,
    [key]: isArray ? [...aggragation[key] || , val] : val
    };
    }, {});

    console.log(gdpr);








    share|improve this answer























    • @customcommander - Yep. It should always be an array, even if it has one item. Fixed.
      – Ori Drori
      Nov 11 at 5:11










    • I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
      – Sylvain Girard
      Nov 11 at 6:18













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:






    const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];

    const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
    const isArray = name.includes('[');
    const key = name.replace(/[.+]/g, '');
    const val = value === 'true';

    return {
    ...aggragation,
    [key]: isArray ? [...aggragation[key] || , val] : val
    };
    }, {});

    console.log(gdpr);








    share|improve this answer














    For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:






    const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];

    const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
    const isArray = name.includes('[');
    const key = name.replace(/[.+]/g, '');
    const val = value === 'true';

    return {
    ...aggragation,
    [key]: isArray ? [...aggragation[key] || , val] : val
    };
    }, {});

    console.log(gdpr);








    const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];

    const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
    const isArray = name.includes('[');
    const key = name.replace(/[.+]/g, '');
    const val = value === 'true';

    return {
    ...aggragation,
    [key]: isArray ? [...aggragation[key] || , val] : val
    };
    }, {});

    console.log(gdpr);





    const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];

    const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
    const isArray = name.includes('[');
    const key = name.replace(/[.+]/g, '');
    const val = value === 'true';

    return {
    ...aggragation,
    [key]: isArray ? [...aggragation[key] || , val] : val
    };
    }, {});

    console.log(gdpr);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 5:16

























    answered Nov 10 at 21:33









    Ori Drori

    71k127388




    71k127388












    • @customcommander - Yep. It should always be an array, even if it has one item. Fixed.
      – Ori Drori
      Nov 11 at 5:11










    • I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
      – Sylvain Girard
      Nov 11 at 6:18


















    • @customcommander - Yep. It should always be an array, even if it has one item. Fixed.
      – Ori Drori
      Nov 11 at 5:11










    • I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
      – Sylvain Girard
      Nov 11 at 6:18
















    @customcommander - Yep. It should always be an array, even if it has one item. Fixed.
    – Ori Drori
    Nov 11 at 5:11




    @customcommander - Yep. It should always be an array, even if it has one item. Fixed.
    – Ori Drori
    Nov 11 at 5:11












    I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
    – Sylvain Girard
    Nov 11 at 6:18




    I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
    – Sylvain Girard
    Nov 11 at 6:18












    up vote
    0
    down vote













    Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string other, then we can assume that it is part of the other form collection structure?



    const seializedForm = $(event.currentTarget.form).serializeArray();

    const gdpr = seializedForm.reduce((aggragation, option) => {
    if (isInArrayOfOptions(option)) {
    return {
    ...aggragation,
    /* Return a new array combining the current with the next option.value*/
    'other': [...aggragation.other, ...[option.value === 'true']]
    }
    }
    return {
    ...aggragation,
    [option.name]: option.value === 'true'
    }
    }, {});





    share|improve this answer

























      up vote
      0
      down vote













      Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string other, then we can assume that it is part of the other form collection structure?



      const seializedForm = $(event.currentTarget.form).serializeArray();

      const gdpr = seializedForm.reduce((aggragation, option) => {
      if (isInArrayOfOptions(option)) {
      return {
      ...aggragation,
      /* Return a new array combining the current with the next option.value*/
      'other': [...aggragation.other, ...[option.value === 'true']]
      }
      }
      return {
      ...aggragation,
      [option.name]: option.value === 'true'
      }
      }, {});





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string other, then we can assume that it is part of the other form collection structure?



        const seializedForm = $(event.currentTarget.form).serializeArray();

        const gdpr = seializedForm.reduce((aggragation, option) => {
        if (isInArrayOfOptions(option)) {
        return {
        ...aggragation,
        /* Return a new array combining the current with the next option.value*/
        'other': [...aggragation.other, ...[option.value === 'true']]
        }
        }
        return {
        ...aggragation,
        [option.name]: option.value === 'true'
        }
        }, {});





        share|improve this answer












        Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string other, then we can assume that it is part of the other form collection structure?



        const seializedForm = $(event.currentTarget.form).serializeArray();

        const gdpr = seializedForm.reduce((aggragation, option) => {
        if (isInArrayOfOptions(option)) {
        return {
        ...aggragation,
        /* Return a new array combining the current with the next option.value*/
        'other': [...aggragation.other, ...[option.value === 'true']]
        }
        }
        return {
        ...aggragation,
        [option.name]: option.value === 'true'
        }
        }, {});






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 21:33









        Kyle

        28914




        28914






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243464%2fstring-to-indexed-property%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues