Problem
I am trying to push a returning variables value into an array. This is my code, however I'm returning an empty array and am not sure what's wrong.
JavaScript
var my_arr = [];
function foo() {
var unitValue = parseFloat($('#unitVal1').val());
var percentFiner = parseFloat($('#percent1').val());
var total = unitValue * 1000;
return my_arr.push({
unit: unitValue,
percent: percentFiner
});
}
return my_arr.push({
unit: unitValue,
percent: percentFiner});
This isn't returning the new Array - this is returning the new length of the Array! Split these out:
my_arr.push({
unit: unitValue,
percent: percentFiner});
return my_arr;
Array.push
returns a length of the changed array, not the array itself
See the Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With