帮助我修复代码,使其以我想要的方式工作。
我有一个数组数组(容器),每个容器已经按每个容器的最大重量3300进行了排序。
条件: 如果容器C有可用空间,并且容器B中的对象具有与容器C中的对象相同的容器级,则需要从数组C开始并将对象从容器B移至容器C,将其转移。并对容器B重复此操作,以相同条件从容器A转移对象。例如:
const first = [[{serial: '1', weight: 300, stage: 2}], [{serial: '2', weight: 3000, stage: 4}]]
const second = [[{serial: '3', weight: 300, stage: 1}], [{serial: '4', weight: 3000, stage: 2}], [{serial: '5', weight: 2700, stage: 3}]]
// result
// updatedFirst: [[], [{serial: '2', weight: 3000, stage: 4}]]
// updatedSecond: [[{serial: '3', weight: 300, stage: 1}], [{serial: '1', weight: 300, stage: 2}, {serial: '4', weight: 3000, stage: 2}], [{serial: '5', weight: 2700, stage: 3}]]
我将first[0]
元素转移到second[1]
是因为second[1]
具有可用空间并且具有相同的容器阶段。
我设法进行转移,但并非如我所愿,转移对象后有很多可用空间。例如,如果在容器B中有1000个可用空间,并且在容器A中有2个权重为500和1000的对象,则我的代码将传输一个权重为500的对象,但我希望该权重为1000的对象被传输。帮助解决问题。
const a = [[{serial: 'a1', weight: 2100.7, stage: 1}, {serial: 'a2', weight: 946.22, stage: 2}], [{serial: 'a3', weight: 1500, stage: 3}, {serial: 'a4', weight: 990, stage: 3}, {serial: 'a5', weight: 399.1, stage: 4} ]]
const b = [[{serial: 'b1', weight: 1999.7, stage: 1}, {serial: 'b2', weight: 1199.22, stage: 2}], [{serial: 'b3', weight: 10.2, stage: 3}, {serial: 'b4', weight: 599.1, stage: 4}]]
const c = [[{serial: 'c1', weight: 1000, stage: 1}, {serial: 'c2', weight: 59.22, stage: 2}], [{serial: 'c3', weight: 0.2, stage: 3}, {serial: 'c4', weight: 3099.1, stage: 4}]]
const updatedC = [...c]
const updatedB = [...b]
const updateContainers = (arr, from, to) => {
let index = 0;
let counter = 0;
const total = {};
arr.forEach((container, i) => {
counter = 0;
container.forEach(el => {
const weight = +(counter += el.weight).toFixed(2);
total[i] = weight;
});
});
Object.keys(total).map((key, i) => {
let freeSpace = 3300 - total[key];
if (freeSpace > 0) {
const stages = arr[i].map(el => el.stage);
const containerStages = {
min: Math.min(...stages),
max: Math.max(...stages)
};
from.forEach((cont) => {
cont.forEach((item, idx) => {
if ((+item.stage >= containerStages.min && +item.stage <= containerStages.max) && +item.stage <= freeSpace) {
to[i].push(item)
freeSpace -= item.stage
from.forEach((elem, elemI) => {
elem.forEach((value, valueI) => {
if (value.serial === item.serial) from[elemI].splice([valueI], 1)
})
})
}
})
})
}
});
};
updateContainers(c, b, updatedC)
updateContainers(b, a, updatedB)
console.log(updatedC)
console.log(updatedB)
console.log(a)
// My result:
// a[0] free space: 1199.3 a[1] free space: 810
// b[0] free space: 1154.56 - not good; b[1] free space: 2341.8 - not good
// c[0] free space: 241.08 - good; c[1] free space: 190.5 - good
// Expected Result
// a[0] free space: 2353.7; a[1] free space: 2900.9
// a [[ {serial: 'a2', weight: 946.22, stage: 2}], [{serial: 'a5', weight: 399.1, stage: 4} ]]
// b[0] free space: 0.07 - good; b[1] free space: 210.9 - good
// b [[{serial: 'a1', weight: 2100.7, stage: 1}, {serial: 'b2', weight: 1199.22, stage: 2}], [{serial: 'a3', weight: 1500, stage: 3}, {serial: 'a4',weight: 990, stage: 3}, {serial: 'b4', weight: 599.1, stage: 4}]]
// c[0] free space: 241.09 - good; c[1] free space: 190.5 - good
// c [[{serial: 'b1',weight: 1999.7, stage: 1}, {serial: 'c1', weight: 1000, stage: 1}, {serial: 'c2', weight: 59.22, stage: 2}], [{serial: 'c3', weight: 0.2, stage: 3},{serial: 'b3', weight: 10.2, stage: 3}, {serial: 'c4', weight: 3099.1, stage: 4}]]```