简体   繁体   中英

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM