I'm encountering an issue while trying to reduce an array within an object. The error message I receive is:
push is not a function
To begin, I initialized my arrays as empty and created an add function to use as the first argument:
function add(a,b) {
return a + b;
}
var navBarArray = [];
var listArray = [];
var mapping = {
".navbar": navBarArray,
".list-group": listArray
};
My attempt to apply this on the mapping object resulted in errors:
var mapping = {
".navbar": Math.round(navBarArray.reduce(add, 0)),
".list-group": listArray
};
Unfortunately, I still receive the error 'push is not a function' in my console. As shown below, I have a function that adds values to the array. While I can further reduce it inside the function, it restricts access to the variable and increases the complexity of the function:
Object.keys(mapping).forEach(function(selector) {
$(selector).hover(function(evt) {
console.log('mapping', mapping);
console.log('selector', selector);
enteredTime = new Date();
}, function() {
var ctime = new Date();
var time = (ctime.getTime() - enteredTime.getTime()) / 1000;
mapping[selector].push(time);
// *********** this works but not where I need it to*******
var reduce = Math.round(navBarArray.reduce(add, 0));
console.log(reduce);
});
})