The function is supposed to add objects to a nested array(Items) in an array of objects and checks whether the object already exists. If it does exist it just increments the quantity, otherwise it should add a new item. However, once the nested array (Items) has a second object, it creates new objects instead of incrementing even if the condition (array[i].items[x].description == _description ) should return true. The _description and _quantity values are passed from user inputs.
var array = [];
AddCustomItem = function (_title, _description) {
if (array.length > 0) {
for (var i in array) {
if (array[i].RoomName === "Other") {
for (var x in array[i].Items) {
if (array[i].Items[x].description == _description) {
array[i].Items[x].quantity += parseInt(_quantity);
break;
} else {
array[i].Items.push({
category : "Other",
description : _description,
quantity : parseInt(_quantity)
});
break;
};
};
break;
};
};
} else {
array.push({
RoomName : "Other",
Items : [{
category : "Other",
description : _description,
quantity : parseInt(_quantity)
}
]
});
};
var result = array;
return result;
};