[
{
id: 1515665095835,
slots: [
{
id: 1,
name: 'Thuja occidentalis'
},
{
id: 2,
name: 'Fraxinus nigra'
},
{
id: 3,
name: 'Populus grandidentata'
},
{
id: 4,
name: 'Populus grandidentata'
},
{
id: 5,
name: 'Populus tremuloides'
},
{
id: 6,
name: 'Fagus grandifolia'
},
{
id: 7,
name: 'Betula lenta'
},
{
id: 8,
name: 'Betula papyrifera'
}
]
},
{
id: 1515665145575,
slots: [
{
id: 9,
name: 'Acer rubrum'
},
{
id: 10,
name: 'Acer saccharinum'
},
{
id: 11,
name: 'Quercus velutina'
},
{
id: 12,
name: 'Quercus coccinea'
},
{
id: 13,
name: 'Pinus rigida'
},
{
id: 14,
name: 'Picea glauca'
},
{
id: 15,
name: 'Juglans nigra'
},
{
id: 16,
name: 'Salix nigra'
}
]
},
{
id: 1515665167326,
slots: [
{
id: 17,
name: 'Crataegus'
},
{
id: 18,
name: 'Tsuga canadensis'
},
{
id: 19,
name: 'Carya cordiformis'
},
{
id: 20,
name: 'Carya glabra'
},
{
id: 21,
name: 'Carya ovata'
},
{
id: 22,
name: 'Ostrya virginiana'
},
{
id: 23,
name: 'Carpinus caroliniana'
},
{
id: 24,
name: 'Larix laricina'
}
]
},
]
- Transform each slot property to have groups of 4 slots
const slotsOf4 = _.map(s, el => ({id: el.id, slots: _.chunk(el.slots, 4)}))
const SlotsOf4Two = _.map(s, ({id, slots}) => ({id, slots:_.chunk(slots, 4)}))
- Based on some logic we can invoke this function for differential chunk size
function slotsChunksOf(array, n) {
return _.map(array, ({id, slots}) =>
({id, slots: _.chunk(slots, n)})
);
}
- Based on this newly created array I want to find the slot with id = 15
_.chain(slotsOf4)
.map(el => el.slots)
.flattenDeep()
.find(slot => slot.id === 15)
.value()
//Now we can create a function that takes a newly selected tree on the interface
function changeSelectedTree(trees, newTreedId) {
return _.chain(trees)
.map(el => el.slots)
.flattenDeep()
.find(slot => slot.id === newTreedId)
.value()
}
class TreePage {
constructor(trees) {
this.trees = trees;
this.selectedTree = trees[0].slots[0][0];
}
changeSelectedTree(newTreedId) {
const newSelected = _.chain(this.trees)
.map(el => el.slots)
.flattenDeep()
.find(slot => slot.id === newTreedId)
.value();
this.selectedTree = newSelected;
return newSelected
}
}
class TreePage {
constructor(trees) {
this.trees = trees;
this.selectedTree = trees[0].slots[0][0];
}
changeSelectedTree(newTreedId) {
const newSelected = _.chain(this.trees)
.map(el => el.slots)
.flattenDeep()
.find(slot => slot.id === newTreedId)
.value();
this.selectedTree = newSelected;
return newSelected
}
}
- Next, look into to using a better approach
this article