This is my data:
JavaScript
x
2
1
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}]
2
How can I transform this data to grouping each pet according to its species and gender like like this:
JavaScript
1
2
1
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: []},]}]
2
My code is:
JavaScript
1
32
32
1
const species = pets.reduce((acc, curr) => {
2
let item = acc.find(
3
(item) =>
4
item.spesies === curr.species || item.gender === curr.gender
5
);
6
7
if (item) {
8
item.genders.push({
9
gender: curr.gender,
10
pets: [
11
{
12
id: acc.id,
13
name: curr.name
14
}
15
]
16
});
17
} else {
18
acc.push({
19
species: curr.species
20
genders: [
21
gender: curr.gender,
22
pets: [
23
{
24
id: acc.id,
25
name: curr.name
26
}
27
]
28
]
29
});
30
}
31
return acc;}, []);
32
the result from my code is:
JavaScript
1
2
1
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)}]}]
2
I expect the final result is like the second array above. Does any one know how to do this better and explaining what I’m missing. Thanks!!
Advertisement
Answer
I think visualizing the problem with the images was clearer, but okay…
Anyway I wrote the function below which should be the simplest to understand.
JavaScript
1
54
54
1
const pets = [
2
{id: 1, name: 'Susi' , species: 'dog', gender: 'female'},
3
{id: 2, name: 'Mini' , species: 'dog', gender: 'female'},
4
{id: 3, name: 'Jojo' , species: 'cat', gender: 'male'},
5
{id: 4, name: 'Band' , species: 'cat', gender: 'male'},
6
{id: 5, name: 'Tommy' , species: 'rabbit', gender: 'male'}
7
]
8
9
function createNewData(pets){
10
// newList that we will be constructing
11
const newData = []
12
13
//Search in dictionary and get all de Unique Species types
14
const speciesList = pets
15
.map(({ species }) => species)
16
.filter((v, i, a) => a.indexOf(v) === i)
17
18
//For each unique species that we found in pets
19
for(let i = 0; i < speciesList.length; i++){
20
//Inside this loop we will construct each specie object
21
22
const specie = speciesList[i]
23
const newSpeciesObj = {species: specie, genders: [] }
24
25
//The male list will be inside the maleGenderObj
26
const maleList = []
27
//The female list will be inside the femaleGenderObj
28
const femaleList = []
29
30
for(let i = 0; i < pets.length; i++){
31
//Inside this loop we add items to the maleList and femaleList,
32
//if they are of the current specie
33
if(pets[i].species == specie) {
34
if (pets[i].gender == 'male'){
35
maleList.push(pets[i])
36
} else {
37
femaleList.push(pets[i])
38
}
39
}
40
}
41
//Here we push the maleGenderObject into genders list
42
newSpeciesObj.genders.push({gender: 'male', pets: maleList})
43
44
//Here we push the femaleGenderObject into genders list
45
newSpeciesObj.genders.push({gender: 'female', pets: femaleList})
46
47
//We do this for all the species we found at the original data
48
newData.push(newSpeciesObj)
49
}
50
51
return newData
52
}
53
54
console.log(createNewData(pets))
I’m currently reviewing your code to try to make it work!