Hello everyone, I have a collection of JSON objects that are initially set to null in the program like this:
var jsonToSaveToDB = [
{
ProductID: null,
Quantity: null,
TotalPrice: null
}
];
As the program progresses, a function fills up this array with actual data as shown below:
var jsonToSaveToDB = [
{
ProductID: 1,
Quantity: 4,
TotalPrice: 12.80
},
{
ProductID: 2,
Quantity: 2,
TotalPrice: 12.80
},
..... other elements of the array
{
ProductID: null,
Quantity: null,
TotalPrice: null
}
];
I intend to retrieve and display the last element of the array since it contains null values for all its properties.
However, there's a limitation - I cannot remove an element from the array. If you're curious about my code structure, take a look below:
<script>
var jsonToSaveToDB = [
{
ProductID: null,
Quantity: null,
TotalPrice: null
}
];
///some codes....
//....
$(document).ready(function(){
function checkoutNow() {
$.each(carted_prods, function (index, element) {
var newObj = {
ProductID: parseInt(element.itemID),
Quantity: parseInt(element.itemQuantity),
TotalPrice: parseFloat(element.itemPrice)
};
jsonToSaveToDB.unshift(newObj);
});
console.log(jsonToSaveToDB);
if (jsonToSaveToDB.length > 0) {
$.ajax({
url: '/POS/POS/CheckoutProducts',
data: { json: JSON.stringify(jsonToSaveToDB) },
contentType: "application/json",
success: function (result) {
console.log(result);
}
});
}
//console.log("Checkout now.");
}
});
</script>