Javascript -按属性过滤和/或分组对象数组

vngu2lb8  于 2023-01-24  发布在  Java
关注(0)|答案(4)|浏览(203)

我有一个对象数组

{
    "events": [
        {
            "id": 0,
            "description": "Monthly play with friends",
            "categoryId": 0
        },
        {
            "id": 1,
            "description": "Buy Goods",
            "categoryId": 2
        },
        {
            "id": 2,
            "description": "Yoga",
            "categoryId": 1
        },
        {
            "id": 3,
            "description": "Lunch",
            "categoryId": 0
        },
        {
            "id": 4,
            "description": "Feed wild & free Bearded Dragons",
            "categoryId": 3
        },
        {
            "id": 5,
            "description": "Come to help our little friends",
            "categoryId": 3
        },
        {
            "id": 6,
            "description": "Come and share your expertise",
            "categoryId": 3
        },
        {
            "id": 7,
            "description": "Dinner with Brother",
            "categoryId": 2
        }
    ]
}

我需要显示按CategoryId分组的项目,将CategoryID显示为组的标题,然后在其中列出该类别的项目。
CategoryId 0每月与朋友一起玩午餐
类别Id 1瑜伽
CategoryId 2购买商品与兄弟共进晚餐
等等...

moiiocjp

moiiocjp1#

尝试使用Array.prototype.reduce()函数:

let ans = events.reduce((cum, x) => {
  if (cum['category ' + x.categoryId] == null) {
    cum['category ' + x.categoryId] = [x.description];
    return cum;
  } else {
    cum['category ' + x.categoryId].push(x.description);
    return cum;
  }
}, {});
sbdsn5lh

sbdsn5lh2#

您可以使用自定义功能使用按类别分组或其他键。我已经创建了一个自定义功能,您可以轻松使用。

const groupedBy = (array) => {
  const grouped = {}
  let types = [...new Set(array.map(({
    type
  }) => type))]
  types.forEach((type) => {
    grouped[type] = array.filter(a => a.type === type)
  })
  return grouped
}

groupedBy([{
  name: 'h',
  type: 'user'
}, {
  name: 'r',
  type: 'user'
}, {
  name: 'v',
  type: 'user'
}, {
  name: 'p',
  type: 'admin'
}, {
  name: 'k',
  type: 'admin'
}])
wpx232ag

wpx232ag3#

一个ez for循环就足够了:

const data = { events: ... }

var obj = {};
for (let event of data.events) {
  // if empty, creates a array
  obj[event.categoryId] = obj[event.categoryId] || [];
  // add data in that array
  obj[event.categoryId].push(event);
}

现在,每个categoryId都有一个表,您可以随意显示它

for (let categoryId in obj) {
  console.log(categoryId, obj[categoryId]);
// or
//  console.log(categoryId, obj[categoryId].map(x => x.description).join(','));
}
mpgws1up

mpgws1up4#

您需要创建一个索引数量与类别数量相同的二维数组。在每个索引中,将有一个包含与该类别匹配的所有描述的数组
然后需要为数组中的每个对象运行一个for循环,将它们的“description”值存储在相关索引中。
然后,您可以打印索引(将匹配类别)沿着包含的所有描述

var events= [
        {
            "id": 0,
            "description": "Monthly play with friends",
            "categoryId": 0
        },
        {
            "id": 1,
            "description": "Buy Goods",
            "categoryId": 2
        },
        {
            "id": 2,
            "description": "Yoga",
            "categoryId": 1
        },
        {
            "id": 3,
            "description": "Lunch",
            "categoryId": 0
        },
        {
            "id": 4,
            "description": "Feed wild & free Bearded Dragons",
            "categoryId": 3
        },
        {
            "id": 5,
            "description": "Come to help our little friends",
            "categoryId": 3
        },
        {
            "id": 6,
            "description": "Come and share your expertise",
            "categoryId": 3
        },
        {
            "id": 7,
            "description": "Dinner with Brother",
            "categoryId": 2
        }
    ];
var eventGroup = [];
for(var i in events){
    if(parseInt(events[i].categoryId) >= eventGroup.length){
        while(eventGroup.length <= parseInt(events[i].categoryId)){
            eventGroup.push([]);
        }
    }
console.log(eventGroup);
    eventGroup[parseInt(events[i].categoryId)].push(events[i].description);
}
for(var f= 0 ; f< eventGroup.length; f++){
    console.log(f + " " + eventGroup[f]);
}

相关问题