Add items to a list, or update the quantity if already in the list I have several lists that contain Part objects with a partNumber and quantity. When the user selects to add a part to an existing list, I want to check to see if the part is already on the list. If it is, I simply want to add the new quantity to the existing quantity, and if the part is not already on the list, I want to add the entire Part object to the list. This solution works, but it feels verbose.

My question is: Can the forEach and if(!partExistsOnTransfer) code be simplified?

var transfer = this.transferRequests[index],
    transferList = $filter("filter")(
        $this.openTransfers, 
        { id: id },
        true
    ),
    partToAdd = new Part(
        $this.currentPart.partNumber, // partNumber
        transfer.quantity // quantity
    ),
    partExistsOnTransfer = false;


transferList[0].parts.forEach(function (part, indx) {
    if (part.partNumber === partToAdd.partNumber) {
        // if part already exists, add to existing quantity, do not push new part onto list
        partExistsOnTransfer = true;
        part.quantity = (Number(part.quantity) + Number(partToAdd.quantity)).toString();
        return false;
    }
});
if (!partExistsOnTransfer) {
    // if part does not exist, push new part onto list
    transferList[0].parts.push(partToAdd);
}
I am not really sure about the needs but when you see something like this it should make you question if there is a better data structure.  If you stored the parts in an Object for example keyed by partNumber the code is more algorithmically efficient (O(1) vs O(N)) and a bit cleaner.

var newPartNumber = partToAdd.partNumber,
    existingPart = parts[newPartNumber];

if(!existingPart) {
    parts[newPartNumber] = partToAdd;
}
else {
    existingPart.quantity = ...;
}


Now if there are other reasons for using an array or using an object keyed by part number wouldn't be useful anywhere else I would use the some
method.  

 var partExistsOnTransfer = transferList[0].parts.some(function (part, indx) {
    if (part.partNumber === partToAdd.partNumber) {
        // if part already exists, add to existing quantity, do not push new part onto list
        part.quantity = (Number(part.quantity) + Number(partToAdd.quantity)).toString();
        return true;
    }
});


Also remember returning false on an Array forEach doesn't make it stop iterating.