У меня есть такой массив
var array = [
{
category: 'smart phone',
name: 'Samsung Galaxy Note 4',
color: 'golden',
index: '1'
},
{
category: 'smart phone',
name: 'Qmobile Noir A10',
color: 'white',
index: '2'
},
{
category: 'smart phone',
name: 'Samsung Galaxy note 8',
color: 'black',
index: '3'
},
{
category: 'laptop',
name: 'Dell inspiron n5110',
color: 'black',
index: '4'
},
{
category: 'laptop',
name: 'Macbook Pro',
color: 'golden',
index: '5'
},
{
category: 'smart phone',
name: 'Samsung Galaxy S5',
color: 'white',
index: '6'
},
{
category: 'smart phone',
name: 'Samsung Galaxy S3',
color: 'white',
index: '7'
},
];
, и я хочу разделить его на основе параллельных похожих элементов.Чтобы добиться этого, если я применяю фильтр.
array.filter(item => item.category === 'smart phone');
он не предоставляет только одновременные элементы, но отфильтровывает все элементы в категории «смартфон».
arr1 = [
{
category: 'smart phone',
name: 'Samsung Galaxy Note 4',
color: 'golden',
index: '1'
},
{
category: 'smart phone',
name: 'Qmobile Noir A10',
color: 'white',
index: '2'
},
{
category: 'smart phone',
name: 'Samsung Galaxy note 8',
color: 'black',
index: '3'
},
{
category: 'smart phone',
name: 'Samsung Galaxy S5',
color: 'white',
index: '6'
},
{
category: 'smart phone',
name: 'Samsung Galaxy S3',
color: 'white',
index: '7'
},
];
Я пытаюсь добиться чего-то подобного.
// output required
// concurrent items of smartphone category
arr1 = [
{
category: 'smart phone',
name: 'Samsung Galaxy Note 4',
color: 'golden',
index: '1'
},
{
category: 'smart phone',
name: 'Qmobile Noir A10',
color: 'white',
index: '2'
},
{
category: 'smart phone',
name: 'Samsung Galaxy note 8',
color: 'black',
index: '3'
},
];
// concurrent items of laptop category
arr2 = [
{
category: 'laptop',
name: 'Dell inspiron n5110',
color: 'black',
index: '4'
},
{
category: 'laptop',
name: 'Macbook Pro',
color: 'golden',
index: '5'
},
];
// again concurrent items of smartphone category
arr3 = [
{
category: 'smart phone',
name: 'Samsung Galaxy S5',
color: 'white',
index: '6'
},
{
category: 'smart phone',
name: 'Samsung Galaxy S3',
color: 'white',
index: '7'
},
];
Как мне достичь этого результата в JavaScript / jQuery.