Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vue.js - How can I modify json response and save it in state?

So for example, my json response looks like:

"name": "John"
"value": "5"

Then I pass it to my state

commit("SET_USER", response.data.user);

But, if name is == "John" i want the value to be 10 and pass it to mutation. Is it possible to do this? I can access value like this response.data.user[0].value

if(response.data.user[0].name == "John") {
              response.data.user[0].value == "10"
            }
            commit("SET_USER", response.data.users); // <-- state.cards
          })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use map function :

commit("SET_USER", response.data.user.map(item=>{
           if(item.name=='John'){
              item.value=10
           }
    return item;
}));

if you already know the index :

let users=response.data.user;
if(users[0].name == "John") {
              users[0].value = 10
            }
commit("SET_USER", users); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...