如何将新对象推送到数组而又不产生重复项?

时间:2019-05-04 05:04:35

标签: javascript arrays oop

我试图创建一个包含多个对象的数组,但是当我将新对象推送到我的数组时,它正在创建最后一个推送对象的数组。我对javascript很陌生,并且相信我正在寻找与Python中的deepcopy类似的东西,但是我一直在读,这对javascript来说不是。

我读到为每个推送创建一个克隆对象将解决我的问题;但是,它不起作用。任何有关如何创建new对象以将我的对象推入数组的建议都将受到赞赏。

class Piece {
    constructor(length, quantity) {
        this.length = length;
        this.quantity = quantity;
    }
}

class Pattern {
    constructor(pattern, remainingLength) {
        this.pattern = pattern;
        this.remainingLength = remainingLength;
    }
}

const  stockLength = 144;
const piecesOrdered = [];
const piece1 = new Piece(19, 15);
const piece2 = new Piece(16, 30);
piecesOrdered.push(piece1);
piecesOrdered.push(piece2);
const allPossiblePatterns = [];

function getBestCuttingSequence() {
    getAllPossiblePatterns();
}

function getAllPossiblePatterns() {
    let previousPattern;
    let pattern = getFirstPattern();
    allPossiblePatterns.push(pattern);
    while(anotherPatternPossible(pattern.pattern)){
        previousPattern = pattern;
        pattern = getNextPattern(previousPattern);
        allPossiblePatterns.push(pattern);
    }
    console.log(allPossiblePatterns);
}

function getFirstPattern() {
    let pattern = [];
    let remainingLength = stockLength;

    for (let piece in piecesOrdered) {
        let quantityNeeded = piecesOrdered[piece].quantity;
        let maxQuantityPossible = Math.floor(remainingLength / piecesOrdered[piece].length);
        let quantity = Math.min(quantityNeeded, maxQuantityPossible);
        remainingLength -= quantity * piecesOrdered[piece].length;
        pattern.push(new Piece(piecesOrdered[piece].length, quantity));
    }

    return new Pattern(pattern, remainingLength);
}

function anotherPatternPossible(previousPattern) {
    for (let piece in previousPattern) {
        if (previousPattern[piece].quantity > 0) {
            return true;
        }
    }
    return false;
}

function getNextPattern(previousPattern) {
    const startIndexAndLoweredPattern = lowerSmallestLength(previousPattern);
    const nextPattern = useRemainingLength(startIndexAndLoweredPattern);
    return nextPattern;
}

function lowerSmallestLength(pattern) {
    let startIndex;
    let loweredPattern = pattern;
    for (let i = loweredPattern.pattern.length -1; i >= 0; i--) {
        if (loweredPattern.pattern[i].quantity > 0) {
            loweredPattern.pattern[i].quantity -= 1;
            loweredPattern.remainingLength += loweredPattern.pattern[i].length;
            startIndex = i + 1;
            break;
        }
    }

    return [startIndex, loweredPattern]
}

function useRemainingLength(startIndexAndLoweredPattern) {
    const startIndex = startIndexAndLoweredPattern[0];
    let loweredPattern = startIndexAndLoweredPattern[1];

    for (let i = startIndex; i < loweredPattern.pattern.length; i++) {
        while (loweredPattern.pattern[i].length <= loweredPattern.remainingLength && loweredPattern.pattern[i].quantity < piecesOrdered[i].quantity) {
            loweredPattern.pattern[i].quantity += 1;
            loweredPattern.remainingLength -= loweredPattern.pattern[i].length;
        }
    }
    let nextPattern = Object.assign({}, loweredPattern);
    return nextPattern;
}

getBestCuttingSequence();

这不是我的完整代码,而是它的简化版本,可产生相同的结果。任何帮助将不胜感激。

预期结果:

[{[{19, 7}, {16, 0}], remainingLength: 11},
{[{19, 6}, {16, 1}], remainingLength: 14},
...,
{[{19, 0}, {16, 0}], remainingLength: 144}]

实际结果:

[{[{19, 0}, {16, 0}], remainingLength: 144},
{[{19, 0}, {16, 0}], remainingLength: 144},
...
{[{19, 0}, {16, 0}], remainingLength: 144}]

1 个答案:

答案 0 :(得分:1)

没有收到您的问题,但您仍在请求deepClone。您可以尝试这样

var a = {b:{g:{54}}};
var clonedA = JSON.parse(JSON.stringify(a));

这解决了deepClone问题。如果您还需要更多,请通过一些小示例澄清您的问题。