A
A
aloky2018-03-28 14:15:42
JavaScript
aloky, 2018-03-28 14:15:42

Sort the list by favorite, how to do it?

There is a list:

{
                    category: 'Посуда',
                    image: 'http://teadrinkers.org/assets/images/products/417/c2cee981a39099c7fa00c9c7797dfe7afae168fb.jpg',
                    title: 'Заголовок',
                    price: '10',
                    year: '2015',
                    country: 'Китай',
                    gram: false
                },
                {
                    category: 'Посуда',
                    image: 'http://teadrinkers.org/assets/images/products/354/42993ea70df676dfc302af7073b2503f581c2132.jpg',
                    title: 'Чашка глина (1)',
                    price: '500',
                    year: '2017',
                    country: 'Япония',
                    gram: false
                },
                {
                    category: 'Улун',
                    image: 'http://teadrinkers.org/assets/images/products/305/8386f5419680011666732dcf3a385a5c197d3ab9.png',
                    title: 'ГАБА улун полусферической скрутки',
                    price: '20',
                    year: '2002',
                    country: 'Китай',
                    gram: true
                },
                {
                    category: 'Красный чай',
                    image: 'http://teadrinkers.org/assets/images/products/433/34abe5f187227d79e0e8f7bb60711817072066f5.jpg',
                    title: 'Чжэн Хэ Сяо Чжун',
                    price: '22',
                    year: '2013',
                    country: 'Китай',
                    gram: true
                }

Let's say we added the category "Red Tea" to cookieor localStoragelist for example: "Red Tea, Oolong" and the list should be in this way first Red Tea > Oolong > everything else.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2018-03-28
@aloky

var list = [{
                    category: 'Посуда',
                    image: 'http://teadrinkers.org/assets/images/products/417/c2cee981a39099c7fa00c9c7797dfe7afae168fb.jpg',
                    title: 'Заголовок',
                    price: '10',
                    year: '2015',
                    country: 'Китай',
                    gram: false
                },
                {
                    category: 'Посуда',
                    image: 'http://teadrinkers.org/assets/images/products/354/42993ea70df676dfc302af7073b2503f581c2132.jpg',
                    title: 'Чашка глина (1)',
                    price: '500',
                    year: '2017',
                    country: 'Япония',
                    gram: false
                },
                {
                    category: 'Улун',
                    image: 'http://teadrinkers.org/assets/images/products/305/8386f5419680011666732dcf3a385a5c197d3ab9.png',
                    title: 'ГАБА улун полусферической скрутки',
                    price: '20',
                    year: '2002',
                    country: 'Китай',
                    gram: true
                },
                {
                    category: 'Красный чай',
                    image: 'http://teadrinkers.org/assets/images/products/433/34abe5f187227d79e0e8f7bb60711817072066f5.jpg',
                    title: 'Чжэн Хэ Сяо Чжун',
                    price: '22',
                    year: '2013',
                    country: 'Китай',
                    gram: true
                }]

var data = 'Красный чай, Улун' //ваши данные из локалсторадж
var order = data.split(", ") //те же данные, но в массиве

var res = [] //сюда выводим

order.forEach(item => res.push(list.filter(l => l.category == item))) //данные по списку
res.push(list.filter(l => order.indexOf(l.category) == -1)) //все остальное
res = [].concat.apply([], res); //склеиваем

C
coderisimo, 2018-03-28
@coderisimo

https://codepen.io/coderisimo/pen/MVrqEd?editors=1111

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question