I am working with a 'data' props object that looks like this:
data = [
{
"label":"gender",
"options":[
{"text":"m","value":0},
{"text":"f","value":1},
{"text":"x", "value":null}
]
},
{
"label":"age",
"options":[
{"text":"<30", "value":0},
{"text":"<50","value":1},
{"text":">50","value":3}
]
}
]
In a computed property, I want to create a new array that is identical to the data prop, except that I need to multiply the value in the options array by 2. Previously, in plain JavaScript, I achieved this using the following code:
data.forEach(item => {
item.options.forEach(option => {
if (option.value !== null && option.value !== 0) {
option.value *= 2;
}
})
});
Now, I am attempting to do this within a computed property using .map(), so that it does not modify my original data props. However, I am struggling to implement it correctly. Here is my attempt:
computed: {
doubledValues() {
var array = this.data.map((item) => {
//...
item.options.map((option) => {
//... option.value * 2;
});
});
return array;
}
}