为了账号安全,请及时绑定邮箱和手机立即绑定

按类别排列的产品阵列

按类别排列的产品阵列

我正在尝试按类别排列这一系列产品。现在,我得到了每个类别的计数,但我不知道如何制作这个二维数组输出。


let products = [

  {name: 'Tequila', category: 'drink'}, 

  {name: 'Beer', category: 'drink'}, 

  {name: 'Burger', category: 'food'},

  {name: 'Shawarma', category: 'food'}, 

  {name: 'Wine', category: 'drink'},

  {name: 'Gelatto', category: 'dessert'}

];


/*expected ouput


let arranged = [[

  {name: 'Tequila', category: 'drink'}, 

  {name: 'Beer', category: 'drink'},

  {name: 'Wine', category: 'drink'}

], [

  {name: 'Burger', category: 'food'},

  {name: 'Shawarma', category: 'food'}

], [

  {name: 'Gelatto', category: 'dessert'}

]];


*/

这是我现在的代码:


let products = [

  {name: 'Tequila', category: 'drink'}, 

  {name: 'Beer', category: 'drink'}, 

  {name: 'Burger', category: 'food'},

  {name: 'Shawarma', category: 'food'}, 

  {name: 'Wine', category: 'drink'},

  {name: 'Gelatto', category: 'dessert'}

];


let arranged = {};


products.map(x => arranged[x.category] = 1 + (arranged[x.category] || 0));


console.log(arranged);


查看完整描述

3 回答

?
慕妹3146593

TA贡献1575条经验 获得超9个赞

您可以通过使用对象存储属于每个类别的元素来减少数组来对元素进行分组。要获得分组的类别,我们可以使用Object.values.


let products = [{name: 'Tequila', category: 'drink'}, 

{name: 'Beer', category: 'drink'}, 

{name: 'Burger', category: 'food'},

{name: 'Shawarma', category: 'food'}, 

{name: 'Wine', category: 'drink'},

{name: 'Gelatto', category: 'dessert'}];

const res = Object.values(

  products.reduce((acc,curr)=>(

    (acc[curr.category] = acc[curr.category] || []).push(curr), acc

  ), {})

);

console.log(res);


查看完整回答
反对 回复 2023-01-06
?
桃花长相依

TA贡献1598条经验 获得超8个赞

试试这个。我正在使用Array.prototype.reduce()按类别键对数组中的对象进行分组,然后您可以获得所需的数据Object.values()

let products = [{

    name: 'Tequila',

    category: 'drink'

  },

  {

    name: 'Beer',

    category: 'drink'

  },

  {

    name: 'Burger',

    category: 'food'

  },

  {

    name: 'Shawarma',

    category: 'food'

  },

  {

    name: 'Wine',

    category: 'drink'

  },

  {

    name: 'Gelatto',

    category: 'dessert'

  }

];




let group = products.reduce((r, a) => {



  r[a.category] = [...r[a.category] || [], a];

  return r;

}, {});

console.log(Object.values(group));





查看完整回答
反对 回复 2023-01-06
?
慕沐林林

TA贡献1789条经验 获得超9个赞

我使用 reducer 遍历数组。为了构建组,我使用存储类别的索引数组。在添加新项目之前,我会查看此数组中是否存在该类别。如果是这样,我获取索引并将该项目添加到该索引组。否则,我会创建一个新的组数组来添加该项目。


let products = [{name: 'Tequila', category: 'drink'}, 

{name: 'Beer', category: 'drink'}, 

{name: 'Burger', category: 'food'},

{name: 'Shawarma', category: 'food'}, 

{name: 'Wine', category: 'drink'},

{name: 'Gelatto', category: 'dessert'}];


let categories=[];

let res = products.reduce((new_object, current_item) => {

    index = categories.indexOf(current_item.category);


    if (index==-1) {

        categories.push(current_item.category)

        new_object.push([current_item]);

    } else {

        new_object[index].push(current_item);

    }

    return new_object;

}, []);


console.log(res);


查看完整回答
反对 回复 2023-01-06
  • 3 回答
  • 0 关注
  • 12 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信