Monday, May 20, 2024
87
rated 0 times [  92] [ 5]  / answers: 1 / hits: 25670  / 6 Years ago, sun, january 21, 2018, 12:00:00

I have a data object in vue that looks like this



rows[
0 {
title: my title,
post: my post text,
public: false,
info: some info
},
1 {
title: my title,
post: my post text
public: true,
info: some info
},
2 {
title: my title,
post: my post text
public: false,
info: some info
}
]


I then copy that object and remove certain properties if needed before posting the object to my backend like this:



var postData = this.rows;
postData.forEach(function(o) {

if (o.public === true) {
delete o.info;
}
});

var uploadData = {};
uploadData.blogpost = postData;
axios({
method: 'post',
url: myUrl,
responseType: 'json',
data: uploadData
})


The problem is that delete o.info; will also remove the property from my vm root data, and I dont understand why since I created a new varible/copied the root data into that one. So how can I remove certain object properties from my data before posting it without altering my root data vm in vue ?


More From » ecmascript-6

 Answers
30

You need to take a copy of your data by cloning it. There are various ways of cloning the data, I would recommend using lodash's function, cloneDeep


import _ from 'lodash'
...
postDataCopy = _.cloneDeep(postData)

Then you can modify postDataCopy as you like without modifying the original.


[#55412] Wednesday, January 17, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
alejandro questions
Mon, Jul 18, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Thu, Sep 10, 20, 00:00, 4 Years ago
;