Answer the question
In order to leave comments, you need to log in
How to create an array with objects from another array?
Vue has an array with objects:
posts: [
{
title: 'Тестовый пост',
content: 'lorem lorem',
category: 'lifestyle'
},
{
title: 'Тестовый пост',
content: 'lorem lorem',
category: 'lifestyle'
},
{
title: 'Тестовый пост',
content: 'lorem lorem',
category: 'business'
},
{
title: 'Тестовый пост',
content: 'lorem lorem',
category: 'travel'
},
{
title: 'Тестовый пост',
content: 'lorem lorem',
category: 'travel'
},
]
}
Нужно создать отдельный массив с постами каждой отдельной категории (travel, lifestyle, business). Как это лучше сделать?
Answer the question
In order to leave comments, you need to log in
computed: {
groupedPosts() {
return this.posts.reduce((acc, n) => {
(acc[n.category] = acc[n.category] || []).push(n);
return acc;
}, {});
},
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question