Can we dynamically add a string
to an array based on the quantity entered by the user in a field?
For example, if a user adds an item to their shopping basket, the function()
will add the SKU string to the array. However, if the user then updates the quantity of the item in the basket (e.g. changes it to 5), should we push 5 SKU strings into the array?
Is this feasible?
I initially tried using a unary operator to convert the string to a number in the code snippet below, but it seems to return NaN
likely due to the fact that skuField is a string
For example: SKU 321005Y-00100-ITM
console.log('Pushing SKU based on qtyField ', [item.qtyField + (+item.skuField)]);
Object example: This is how my object appears when an item with a quantity of 3 is added to the basket.
var updateCartItem = {
Products: [
{
"product_idField":"96031",
"skuField":"321005Y-00100-ITM",
"nameField":"Cooper Sunglasses",
"priceField":"75.0000",
"qtyField":3,
"qtyFieldSpecified":true,
"$$hashKey":"object:8"
}
]
}
Code snippet:
for (var i = 0; i < updateCartItem.Products.length; i++) {
var element = updateCartItem.Products[i];
console.log([element.skuField]) // this will only push 1 sku (should be 3)
}
Therefore, based on qtyField: 3
, I anticipate having
["321005Y-00100-ITM", "321005Y-00100-ITM", "321005Y-00100-ITM"]
. If qtyField
is 5 or 100, then the array string should contain the same number of SKU strings as the quantity specified.