Simon H
Simon H

Reputation: 21007

lodash: error chaining mapValues

I'm struggling to chain _.mapValues. This is the code

var result = _.mapValues(results[0], function(val, qname) {
    return (results[1].hasOwnProperty(qname)) ? _.assign(val, results[1][qname]) : val
})
.mapValues(function(r) {r.total = addFields(r); return r;})

which is resulting in a runtime TypeError: Object #<Object> has no method 'mapValues' for the second mapValues. The first mapValues is working fine.

Upvotes: 2

Views: 1216

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

In this case, you need use method _.chain, like this

var result = _.chain(results[0])
   .mapValues(function(val, qname) {
       return (results[1].hasOwnProperty(qname)) ? _.assign(val, results[1][qname]) : val
   })
   .mapValues(function(r) {r.total = addFields(r); return r;})
   .value();

Upvotes: 3

Related Questions