0

I am trying to reduce an array inside of an object. I am getting back

push is not a function 

I have started my array as empty and created a add function to pass in as the first argument.

function add(a,b) {
        return a +b;
}

var navBarArray = [];
var listArray = [];

var mapping = {
  ".navbar": navBarArray,
  ".list-group": listArray
};

I tried this approach on the mapping object but it creates errors

var mapping = {
  ".navbar": Math.round(navBarArray.reduce(add,0) ),
  ".list-group": listArray
};

However, I get push is not a function back in my console.

Below is my function that passes values to the array. I can create a variable inside the function and reduce it there. However, that limits access to my variable and will bloat my function as I continue.

  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);
        });
    })
7
  • Maybe mapping[selector] is undefined...? Commented Sep 22, 2015 at 18:02
  • Which line is getting the error? Calling reduce shouldn't report an error about push. Commented Sep 22, 2015 at 18:03
  • Maybe make sure that mapping[selector] is typeof "array"? Commented Sep 22, 2015 at 18:03
  • Result of reduce is not an array in your case. And it's not clear what you want it to be actually. Commented Sep 22, 2015 at 18:03
  • 2
    The second var mapping = ... snippet sets the value of the .navbar key to a number, but then you try to use it like an array with mapping[selector].push(...). I think you just need to stick with the first definition of mapping. Commented Sep 22, 2015 at 18:04

1 Answer 1

2

Change your mapping object so it has separate places for the array and total:

var mapping = {
    ".navbar": {
        total: 0,
        times: []
    },
    ".list-group": {
        total: 0,
        times: []
    }
}

Then you do mapping[selector].times.push(time), and put the total with:

mapping[selector].total = mapping[selector].times.reduce(add, 0);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I follow this approach. From what I read I could not place a reduce in the object.
You must have misread or misunderstood. There's nothing special about reduce, it's just a function call that returns a value. And you can put any value in an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.