【问题标题】:Lodash filter out rougue items from objectLodash 从对象中过滤掉恶意项目
【发布时间】:2017-12-19 20:08:16
【问题描述】:

我正在使用 lodash map 和 uniqby 从 json 文件 events.json 创建一个对象。

我很少收到包含“英国”以外国家/地区的活动。我在结果中不需要它们,我将如何“不映射”不在“英国”中的事件。谢谢

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
}));

我查看了 _.filter,但不确定在 map 函数中的何处使用它。

【问题讨论】:

  • 先使用filter
  • 我希望这一切都可以在 UniqBy & Map 的同一条线上完成。
  • 可以-.filter((event) => event.place.location.country === "United Kingdom")
  • @PaulLeppard 是的,就是这样
  • 什么明星。谢谢!

标签: javascript json node.js object lodash


【解决方案1】:

您可以在地图之前或之后使用过滤器。但在地图之前它会更有用。

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
}));

但问题是为什么你需要 uniq by id?通常 id 是一个应该是 uniq 的属性

【讨论】:

  • 谢谢.. 是的,你会想到,但我的数据文件有时有重复项,我需要删除。
  • 感谢@user184994 加入上述 cmets,但仍然感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2022-10-05
相关资源
最近更新 更多