Reactjs 在Lodash中创建子节点集合

Reactjs 在Lodash中创建子节点集合,reactjs,lodash,Reactjs,Lodash,我有一个有机会的数据结构,每个机会都有注释。我想记下与一个人有关的所有机会的所有笔记,并展示它们。现在我有下面的,这是有效的。但是,我认为必须有一种更简单的方法,我不能使用外部数组,而是从opportunityNotesList返回与.value()相同的结果 const thisNotes = {} const opportunityNotesList = _(opportunities) .map((opportunity, id) => ({id, .

我有一个有机会的数据结构,每个机会都有注释。我想记下与一个人有关的所有机会的所有笔记,并展示它们。现在我有下面的,这是有效的。但是,我认为必须有一种更简单的方法,我不能使用外部数组,而是从opportunityNotesList返回与.value()相同的结果

    const thisNotes = {}
    const opportunityNotesList = _(opportunities)
        .map((opportunity, id) => ({id, ...opportunity}))
        .filter(opportunity => opportunity.linkToContact === id)
        .map(opportunity => {
            _(opportunity.notes)
                .map((note, id) => ({id, ...note}))
                .each(note  => {
                    thisNotes[`${note.id}`] = note
                })
        })

真的只是在寻找一种更优雅的方式来完成这个问题。

我建议不要太优雅。我已经很难跟踪上面的代码了,想象一下另一个开发人员必须拿起它并进行更改

将其分离到自己的助手中,或者最好使用选择器(例如使用
重新选择


我同意,随着时间的推移,这种转变变得相当痛苦——这是我似乎一直在头疼的一件事

使用
.flatMap
避免嵌套循环

 const thisNotes = _(opportunities)
    .filter({linkToContact: id})
    .flatMap('notes')
    // way 1 - if id is really necessary
    .map((note, id) => ({id, ...note}))
    .keyBy('id')
    // way 2 - if id is unnecessary
    .reduce(function(result, note, id) {
        result[id] = note;
        return result;
    }, {});

如果保留
.map()