I have two array's of objects.
a) this.units (holds ALL the info on the units)
b) item.units (holds just the IDs of the units being used)
I am trying to match up the full unit with the unit id. The method below works but it puts the units out of their original order.
The item.unit will have an array of unit ids in a specific order lets say 1,2,3,4
I then need to attach the full unit from this.units to the item.unit but keep the same order 1,2,3,4
This code matches the full unit to the item unit but it uses the order from this.units not item.unit so the end results is something like 4,2,1,3
previewSectionInfo(item) {
const sectionsUnits = this.units.filter((u) => {
return item.units.find(el => el.id == u.id)
})
item.units = sectionsUnits
}
I think I would instead map()
over item.unit
(keeping its order) and return the corresponding record from this.units
.
const item = {};
item.units = [1,3,5];
//const thiss = {};
this.units = [
{id:1,name:"one"},
{id:2,name:"two"},
{id:3,name:"three"},
{id:4,name:"four"},
{id:5,name:"five"}
];
function previewSectionInfo() {
const specificUnits = item.units.map(
function(unitID){
return this.units.find(el => el.id == unitID);
}
)
console.log(specificUnits);
}
previewSectionInfo();