如何在javascript中按类别对数据数组进行分组

荷兰蒂米塞拉

这是我的数据:

pets: [{id: 1, name: Susi, species: dog, gender: female},{id: 2, name: Xixi, species: dog, gender: female},{id: 3, name: Boss, species: rabbit, gender: male},{id: 4, name: Bunny, species: rabbit, gender: male},{id: 5, name: kitty, species: cat, gender: female},{id: 6, name: Garfield, species: cat, gender: male}]

如何将这些数据转换为根据每个宠物的物种和性别对其进行分组,如下所示:

data: [{species: dog, genders: [{gender: male, pets: [{id: 1, name: Susi, species: dog, gender: female},]}, {gender: female, pets: [{id: 2, name: Xixi, species: dog, gender: female},]}, ]},{species: cat, genders: [{gender: male, pets: [{id: 6, name: Garfield, species: cat, gender: male}]},{gender: female, pets: [{id:5, name: kitty, species: cat, gender: female},]},]},{species: rabbit,genders: [{gender: male, pets: [{id: 3, name: Boss, species: rabbit, gender: male},{id: 4, name: Bunny, species: rabbit, gender: male},]},{gender: female, pets: []},]}]

我的代码是:

const species = pets.reduce((acc, curr) => {
let item = acc.find(
  (item) =>
    item.spesies === curr.species || item.gender === curr.gender
);

if (item) {
  item.genders.push({
    gender: curr.gender,
    pets: [
     {
       id: acc.id,
       name: curr.name
     }
    ]
  });
} else {
  acc.push({
    species: curr.species
    genders: [
    gender: curr.gender,
    pets: [
     {
       id: acc.id,
       name: curr.name
     }
    ]
  ]
  });
}
return acc;}, []);

我的代码的结果是:

data: [{species: dog, genders: [{gender: female, pets: Array(1)},{gender: female, pets: Array(1)},]},species: cat, genders: [{gender: male, pets: Array(1)},{gender: female, pets: Array(1)},]},{species: rabbit, genders: [{gender: male, pets: Array(1)},{gender: male, pets: Array(1)}]}]

我希望最终结果就像上面的第二个数组。有谁知道如何更好地做到这一点并解释我所缺少的。谢谢!!

若昂·维索萨

我认为用图像可视化问题更清晰,但好吧......

无论如何,我写了下面的函数,它应该是最容易理解的。

const pets = [
    {id: 1, name: 'Susi' , species: 'dog', gender: 'female'},
    {id: 2, name: 'Mini' , species: 'dog', gender: 'female'},
    {id: 3, name: 'Jojo' , species: 'cat', gender: 'male'},
    {id: 4, name: 'Band' , species: 'cat', gender: 'male'},
    {id: 5, name: 'Tommy' , species: 'rabbit', gender: 'male'}
]

function createNewData(pets){
    // newList that we will be constructing
    const newData = []
    
    //Search in dictionary and get all de Unique Species types
    const speciesList = pets
        .map(({ species }) => species)
        .filter((v, i, a) => a.indexOf(v) === i)
    
    //For each unique species that we found in pets
    for(let i = 0; i < speciesList.length; i++){
        //Inside this loop we will construct each specie object
        
        const specie = speciesList[i]
        const newSpeciesObj = {species: specie, genders: [] }
       
        //The male list will be inside the maleGenderObj
        const maleList = []
        //The female list will be inside the femaleGenderObj
        const femaleList = []

        for(let i = 0; i < pets.length; i++){  
        //Inside this loop we add items to the maleList and femaleList, 
        //if they are of the current specie
            if(pets[i].species == specie) {
                if (pets[i].gender == 'male'){
                    maleList.push(pets[i]) 
                } else {
                    femaleList.push(pets[i])
                }
            }
        }
        //Here we push the maleGenderObject into genders list
        newSpeciesObj.genders.push({gender: 'male', pets: maleList})
        
        //Here we push the femaleGenderObject into genders list
        newSpeciesObj.genders.push({gender: 'female', pets: femaleList})   
        
        //We do this for all the species we found at the original data
        newData.push(newSpeciesObj)    
    }
    
    return newData
}

console.log(createNewData(pets))

我目前正在审查您的代码以尝试使其正常工作!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

按类别对数值进行分组

R:如何按二进制类别对数据帧行进行分组?

创建一个按日期和类别对数据进行分组的图形

使用R按类别对数据进行分组,然后获取所有列的总和

如何在Swift中按日期对数组进行分组?

如何在Django中按类别对主题进行排序?

每个日期按类别对数据进行排序

如何在javascript中对数组进行分组?

如何在JavaScript中对数组条目进行分组

如何在javascript中对数据数组进行分组和排序?

如何根据类别对数组进行排序?

如何按类别对项目进行分组和计数?

如何在JavaScript中按日期对数组进行排序?

如何在Javascript中按属性对数组进行排序

在matplotlib pyplot中,如何按类别对条形图中的条进行分组?

如何按特定类别对geopandas中的多边形进行分组和联合?

按类别对WordPress帖子进行分组

如何按日期对数组数据进行分组

Linq,如何按ID对数据进行分组以及如何在ID中合并数据

如何在CoreData中按日期对数组中的结果进行分组

如何按日期对数组中的项目进行分组?

如何在 angular2+ 中按字母顺序对数组进行排序、分组和划分

如何在 Laravel 8 中按 user_id 对数组进行分组

使用jQuery按类别ID对数组对象进行分组

如何在Python中按6个月对数据进行分组

如何在熊猫中按正数或负数对数据进行分组

如何在熊猫中按日期时间对数据进行分组?

如何在 Azure Cosmos DB 中按日期对数据进行分组?

如何按 Pandas 中的列数对数据进行分组?