Reputation: 1023
I am creating an object from a json file events.json using lodash map and uniqby.
Very rarely I get an event that has a country other than "United Kingdom". I don't need them in the results, how would I go about 'not mapping' the events that are not in the "United Kingdom". Thanks
var _ = require('lodash');
var data = require('./events.json')
var newEventList = _.uniqBy(data.events,'id').map(events => ({
id: events.id ,
name: events.name ,
venue: events.venue.name ,
address: events.place.location.street + " " + events.place.location.city + " " + events.place.location.zip + " " + events.place.location.country
}));
I have looked at _.filter but am not sure of where to use it within the map function.
Upvotes: 0
Views: 30
Reputation: 527
You can use filter before or after map. But before map it will be more useful.
var newEventList = _.uniqBy(data.events,'id').filter(event => event.place.location.country === 'United Kingdom').map(event => ({
id: event.id ,
name: event.name ,
venue: event.venue.name ,
address: event.place.location.street + " " + event.place.location.city + " " + event.place.location.zip + " " + event.place.location.country
}));
But the question is why you need uniq by id? Usually id is a property that should be uniq
Upvotes: 2