我有一个像这样的数组
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中获得此结果。
答案 0 :(得分:2)
您可以减少数组并采用新的数组来更改类别。
结果是一个数组数组,其中每个数组按原始顺序具有相同的类别。
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' }],
result = array.reduce((r, o, i, { [i - 1]: last = {} }) => {
if (last.category !== o.category) r.push([]);
r[r.length - 1].push(o);
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:2)
您可以使用reduce
对对象进行分组。创建一个变量以跟踪先前的category
是什么。如果它与当前category
相同,则将对象推到累加器的最后一个数组。否则,将一个新数组推入累加器。
const 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'},];
let previous;
const output = array.reduce((acc, o) => {
if (previous !== o.category) {
previous = o.category
acc.push([o])
} else {
acc[acc.length - 1].push(o)
}
return acc;
}, [])
console.log(output)
答案 2 :(得分:1)
您可以这样做,将reduce
与slice
结合使用并进行解构:
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'}];
const [[arr1, arr2, arr3]] = array.reduce(([acc, cat], curr) => {
curr.category == cat ? acc[acc.length - 1].push(curr) : acc.push([curr]);
return [acc, curr.category];
}, [[]]).slice(0, -1);
console.log(arr1);
console.log(arr2);
console.log(arr3);
.as-console-wrapper { max-height: 100% !important; top: auto; }
答案 3 :(得分:0)
请确保:您需要分配.filter()
let filteredArray = array.filter(item => item.category === 'smart phone');
仅写array.filter(your function)
不会改变array
。