I would like to keep object keys when reducing a CSV data set using JavaScript's map function (if this is in fact the right approach?)
I have read a top SO thread on preserving keys, but the solutions given require external JS libraries (underscore or lodash). I'd prefer avoiding anything but 'vanila' JS if possible.
For example:
var basket = [{fruit: "apple", veg: "carrot", nut: "peanut"},
{fruit: "pear", veg: "lettuce", nut: "pistachio"},
{fruit: "banana", veg: "radish", nut: "pecan"},
{fruit: "kiwi", veg: "kale", nut: "almond"}]
Yields an array of 3 objects, corresponding to each 'food type' entry above (fruit, veg, nut).
I don't want nuts, so I try using map to filter them out:
var small_basket = basket.map(function(item){
return [
item.fruit,
item.veg
]
});
This approach returns an array of 4 arrays, corresponding to each of the items per food type in my list, e.g. 4 fruits and 4 nuts (so we've effectively eliminated nuts).
My trouble is: now my object keys are gone! Instead, my array now looks like this:
small_basket = Array(3) [
0: Array(2) [
0: "apple"
1: "carrot"
]
1: Array(2) [
0: "pear"
1: "lettuce"
]
2: Array(2) [
0: "bananna"
1: "raddish"
]
]
What can I do differently to preserve object keys?