Answer the question
In order to leave comments, you need to log in
How to rename properties in an object?
There is an object:
{
"id": '',
"name": ''
}
{
"value": '',
"label": ''
}
computed: {
...mapState({
categories: state => state.main_page.NEWS
}),
getCategories() {
let arr = [];
let categories = this.categories;
categories.forEach(function(obj) {
obj['value'] = obj['id'];
obj['label'] = obj['name'];
delete obj['id'];
delete obj['name'];
arr.push(obj)
})
return arr
}
}
Answer the question
In order to leave comments, you need to log in
It is not necessary to give computed properties names that start with "get" - after all, they are properties, not methods.
There is no need for an additional calculated property, which you just have a reference to an array in storage - immediately create a new array, the properties of the elements of which will be renamed as you see fit:
computed: {
categories() {
return this.$store.state.main_page.NEWS.map(n => ({
value: n.id,
label: n.name
}));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question