I have an object structured as follows:
var my_object = { first_item: {important_number: 4}, second_item: {important_number: 6}, }
However, I would like the object to be structured as follows:
{ first_item: 4, second_item: 6, }
I would expect to get this result using _.pluck :
_.pluck(my_object, "important_number")
But it gives me:
[0: 4, 1: 6]
Ok, but I need the actual names of the objects. I messed around and ended up with this:
_.reduce(my_object, function(memo, val, key) { memo[key] = val.content; return memo; }, {});
Which has the desired effect, but not as simple as we would like. Is there a better solution in underscore / lodash, or is it the best it will get?
source share