1 回答
TA贡献1810条经验 获得超5个赞
ItemUnit首先,为where itemUnit.ItemCodeas key 和 slice of UnitNameas value创建一个映射
itemUnitmap := make(map[string][]string)
for _, itemUnit := range itemUnits {
var units []string
for _, unit := range itemUnit.Units {
units = append(units, unit.UnitName)
}
itemUnitmap[itemUnit.ItemCode] = units
}
然后使用 map 获取UnitNameusing的切片item.ItemCode。Item.Units使用可变参数函数添加切片
for orderIndex, order := range unload.Orders {
for itemIndex, item := range order.Items {
if units, ok := itemUnitmap[item.ItemCode]; ok {
unload.Orders[orderIndex].Items[itemIndex].Units = append(unload.Orders[orderIndex].Items[itemIndex].Units, units...)// variadic function used to append slice into slice
}
}
}
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报