I'm transforming this:
[["Apples", "fruitgroup", "123"], ["Bananas", "fruitgroup", "012"], ["Jumbo Jet", "planegroup", "99"], ["Crop duster", "planegroup", "40"], ["Melons", "fruitgroup", "55"]]
into this:
["Apples or Bananas or Melons [x]", "Jumbo Jet or Crop duster [x]"]
Because basically I have to group the child arrays by whatever is in their second index, or [1]
, and then join them with " or "
.
I am successful but I'm afraid this isn't really the best way to acheive this. Isn't there a more simple way to do this?
var filters = [["Apples", "fruitgroup", "123"], ["Bananas", "fruitgroup", "012"], ["Jumbo Jet", "planegroup", "99"], ["Crop duster", "planegroup", "40"], ["Melons", "fruitgroup", "55"]], fieldnames = _.uniq(_.map(filters, function (filter) { return filter[1]; })), groups = _.groupBy(filters, function (filter) { return filter[1]; }), strings = _.map(groups, function (group) { return _.map(group, function (item) { return item[0]; }).join(" or ") + " [x]"; }); console.log(strings);