Accessing to state object property from mutations









up vote
0
down vote

favorite












I'm trying to access a state object property from a mutation in a Vuex Store but I can't find the right syntax :



Here is the state:



state: 
events: [
day: 8
location: NY
,
day: 9:
location: NY

]



And here is the mutation :



SET_EVENT_LIST: (state, list) => 
if(list.day < 10)
list.day = '0'+list.day


state.events = list
,


I want to change the value of each event.day under 10. What am I doing wrong?










share|improve this question



















  • 1




    if the list is an array, so list.day doesn't make sense. you should use list.map function.
    – Madmadi
    Nov 9 at 22:45















up vote
0
down vote

favorite












I'm trying to access a state object property from a mutation in a Vuex Store but I can't find the right syntax :



Here is the state:



state: 
events: [
day: 8
location: NY
,
day: 9:
location: NY

]



And here is the mutation :



SET_EVENT_LIST: (state, list) => 
if(list.day < 10)
list.day = '0'+list.day


state.events = list
,


I want to change the value of each event.day under 10. What am I doing wrong?










share|improve this question



















  • 1




    if the list is an array, so list.day doesn't make sense. you should use list.map function.
    – Madmadi
    Nov 9 at 22:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to access a state object property from a mutation in a Vuex Store but I can't find the right syntax :



Here is the state:



state: 
events: [
day: 8
location: NY
,
day: 9:
location: NY

]



And here is the mutation :



SET_EVENT_LIST: (state, list) => 
if(list.day < 10)
list.day = '0'+list.day


state.events = list
,


I want to change the value of each event.day under 10. What am I doing wrong?










share|improve this question















I'm trying to access a state object property from a mutation in a Vuex Store but I can't find the right syntax :



Here is the state:



state: 
events: [
day: 8
location: NY
,
day: 9:
location: NY

]



And here is the mutation :



SET_EVENT_LIST: (state, list) => 
if(list.day < 10)
list.day = '0'+list.day


state.events = list
,


I want to change the value of each event.day under 10. What am I doing wrong?







javascript vue.js vuejs2 vuex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 23:30









Boussadjra Brahim

3,6702627




3,6702627










asked Nov 9 at 22:40









blickblick

426




426







  • 1




    if the list is an array, so list.day doesn't make sense. you should use list.map function.
    – Madmadi
    Nov 9 at 22:45













  • 1




    if the list is an array, so list.day doesn't make sense. you should use list.map function.
    – Madmadi
    Nov 9 at 22:45








1




1




if the list is an array, so list.day doesn't make sense. you should use list.map function.
– Madmadi
Nov 9 at 22:45





if the list is an array, so list.day doesn't make sense. you should use list.map function.
– Madmadi
Nov 9 at 22:45













1 Answer
1






active

oldest

votes

















up vote
0
down vote













the right syntax should be like :



 const store = new Vuex.Store(
state:
events: [
day: 8
location: "NY"
,
day: 9,
location: "NY"

]
,
mutations:
SET_EVENT_LIST (state, list)

state.events = list.map(l=>
if (l.day < 10)
l.day = '0' + l.day
return l;

else return l;
)


)


Notes



  • the values of location key should be strings


  • Each method in mutations should have this syntax :



     mutations:
    method_name(state,other_parameters)...



or



 mutations:
method_name:(state,other_parameters)=>...



Map function example






let events = [
day: 7,
loc: "NY"
,
day: 8,
loc: "NY"
,
day: 18,
loc: "Paris"
];

let custom_events = events.map(l =>
if (l.day < 10)
l.day = '0' + l.day
return l;
else return l;
);

console.log(custom_events);








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%2f53234180%2faccessing-to-state-object-property-from-mutations%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    the right syntax should be like :



     const store = new Vuex.Store(
    state:
    events: [
    day: 8
    location: "NY"
    ,
    day: 9,
    location: "NY"

    ]
    ,
    mutations:
    SET_EVENT_LIST (state, list)

    state.events = list.map(l=>
    if (l.day < 10)
    l.day = '0' + l.day
    return l;

    else return l;
    )


    )


    Notes



    • the values of location key should be strings


    • Each method in mutations should have this syntax :



       mutations:
      method_name(state,other_parameters)...



    or



     mutations:
    method_name:(state,other_parameters)=>...



    Map function example






    let events = [
    day: 7,
    loc: "NY"
    ,
    day: 8,
    loc: "NY"
    ,
    day: 18,
    loc: "Paris"
    ];

    let custom_events = events.map(l =>
    if (l.day < 10)
    l.day = '0' + l.day
    return l;
    else return l;
    );

    console.log(custom_events);








    share|improve this answer


























      up vote
      0
      down vote













      the right syntax should be like :



       const store = new Vuex.Store(
      state:
      events: [
      day: 8
      location: "NY"
      ,
      day: 9,
      location: "NY"

      ]
      ,
      mutations:
      SET_EVENT_LIST (state, list)

      state.events = list.map(l=>
      if (l.day < 10)
      l.day = '0' + l.day
      return l;

      else return l;
      )


      )


      Notes



      • the values of location key should be strings


      • Each method in mutations should have this syntax :



         mutations:
        method_name(state,other_parameters)...



      or



       mutations:
      method_name:(state,other_parameters)=>...



      Map function example






      let events = [
      day: 7,
      loc: "NY"
      ,
      day: 8,
      loc: "NY"
      ,
      day: 18,
      loc: "Paris"
      ];

      let custom_events = events.map(l =>
      if (l.day < 10)
      l.day = '0' + l.day
      return l;
      else return l;
      );

      console.log(custom_events);








      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        the right syntax should be like :



         const store = new Vuex.Store(
        state:
        events: [
        day: 8
        location: "NY"
        ,
        day: 9,
        location: "NY"

        ]
        ,
        mutations:
        SET_EVENT_LIST (state, list)

        state.events = list.map(l=>
        if (l.day < 10)
        l.day = '0' + l.day
        return l;

        else return l;
        )


        )


        Notes



        • the values of location key should be strings


        • Each method in mutations should have this syntax :



           mutations:
          method_name(state,other_parameters)...



        or



         mutations:
        method_name:(state,other_parameters)=>...



        Map function example






        let events = [
        day: 7,
        loc: "NY"
        ,
        day: 8,
        loc: "NY"
        ,
        day: 18,
        loc: "Paris"
        ];

        let custom_events = events.map(l =>
        if (l.day < 10)
        l.day = '0' + l.day
        return l;
        else return l;
        );

        console.log(custom_events);








        share|improve this answer














        the right syntax should be like :



         const store = new Vuex.Store(
        state:
        events: [
        day: 8
        location: "NY"
        ,
        day: 9,
        location: "NY"

        ]
        ,
        mutations:
        SET_EVENT_LIST (state, list)

        state.events = list.map(l=>
        if (l.day < 10)
        l.day = '0' + l.day
        return l;

        else return l;
        )


        )


        Notes



        • the values of location key should be strings


        • Each method in mutations should have this syntax :



           mutations:
          method_name(state,other_parameters)...



        or



         mutations:
        method_name:(state,other_parameters)=>...



        Map function example






        let events = [
        day: 7,
        loc: "NY"
        ,
        day: 8,
        loc: "NY"
        ,
        day: 18,
        loc: "Paris"
        ];

        let custom_events = events.map(l =>
        if (l.day < 10)
        l.day = '0' + l.day
        return l;
        else return l;
        );

        console.log(custom_events);








        let events = [
        day: 7,
        loc: "NY"
        ,
        day: 8,
        loc: "NY"
        ,
        day: 18,
        loc: "Paris"
        ];

        let custom_events = events.map(l =>
        if (l.day < 10)
        l.day = '0' + l.day
        return l;
        else return l;
        );

        console.log(custom_events);





        let events = [
        day: 7,
        loc: "NY"
        ,
        day: 8,
        loc: "NY"
        ,
        day: 18,
        loc: "Paris"
        ];

        let custom_events = events.map(l =>
        if (l.day < 10)
        l.day = '0' + l.day
        return l;
        else return l;
        );

        console.log(custom_events);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 23:26

























        answered Nov 9 at 22:54









        Boussadjra Brahim

        3,6702627




        3,6702627



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234180%2faccessing-to-state-object-property-from-mutations%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

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20