I understand _.lodash has a map function and a reduce. Could we use either for doing this?
Yes, since reduce
is the most generic iteration method, map
can be expressed via reduce
. It's not necessary here, though.
Is there a simpler way that I could do this? The mapping part seems to be not so simple.
Try to understand what it does. It creates a new object, and iterates the dataMap
to see which properties should be copied whereto.
You can leave that property-mapping-descriptor-loop away and do explicitly/verbosely
var finalT = t.map(function (topic) {
return {
id: topic.topicId,
name: topic.topicName
};
});
// or, with your variable names and lodash:
t2 = _.map(t2, function (topic) {
return {
id: topic.topicId,
name: topic.topicName
};
});