【问题标题】:Lodash _.map to get properties of an array of objectsLodash _.map 获取对象数组的属性
【发布时间】:2019-05-18 01:07:34
【问题描述】:

我有一个这样的对象数组:

    users: [
        { name: 'missing!', ids: ['Not Specified']},
        { name: 'user1', ids: ['C.001', 'C.002', 'C.001.001']},
        { name: 'user2', ids: ['C.001', 'C.002', 'C.004', 'C.001.001']},
        { name: 'user3', ids: ['C.001', 'C.002', 'C.004', 'C.001.001']},
        { name: 'user4', ids: ['C.001', 'C.002', 'C.004', 'C.001.001']}
      ]

我想要实现的是获得一组具有特定 id 的用户名。

例如,如果我传递的 id 为 'C.001.001' ,则应返回 ['user1','user2','user3','user4']。

如果我通过 'C.123214123',应该返回 ['missing!']。

我尝试过但没有奏效的方法:

getUserNamesById(id: string) string[] {
   return _.map(this.users, user => {
         return _.find(user.ids,{id});
});}

这不起作用,也不会处理您找不到 Id 并返回 ['missing'] 的情况(并且它也不会返回 name 属性)。

我是 _.lodash 的新手,我想学习它,但我研究了近 2 个小时,却没有得到任何结果。

如何实现我的输出?

【问题讨论】:

    标签: typescript lodash


    【解决方案1】:

    您需要单独处理“丢失”的情况。

    您需要过滤列表以获取匹配项
    如果有匹配项,请映射以获取名称
    否则获取“缺失”记录

    const users = [
        { name: 'missing!', ids: ['Not Specified']},
        { name: 'user1', ids: ['C.001', 'C.002', 'C.001.001']},
        { name: 'user2', ids: ['C.001', 'C.002', 'C.004', 'C.001.001']},
        { name: 'user3', ids: ['C.001', 'C.002', 'C.004', 'C.001.001']},
        { name: 'user4', ids: ['C.001', 'C.002', 'C.004', 'C.001.001']}
    ];
          
    function find(u, search) {
      const found = _.filter(u, (x) => _.some(x.ids, (id) => id === search));
      if (_.isEmpty(found)) {
        return ['missing!'];
      } else {
        return _.map(found, (f) => f.name);
      }
    }
          
    console.log(find(users, 'C.001.001'));
    console.log(find(users, 'C.123.456'));
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

    【讨论】:

      【解决方案2】:

      您可以使用Array.filtered() 和Array.includes()(或lodash 等效项)来过滤数组,然后使用Array.map() 来获取名称列表:

      const filterById = (id, arr) => {
        const filtered = arr.filter(({ ids }) => ids.includes(id))
          .map(({ name }) => name);
        
        return filtered.length ? filtered : ['missing'];
      }
      
      const users = [{"name":"missing!","ids":["Not Specified"]},{"name":"user1","ids":["C.001","C.002","C.001.001"]},{"name":"user2","ids":["C.001","C.002","C.004","C.001.001"]},{"name":"user3","ids":["C.001","C.002","C.004","C.001.001"]},{"name":"user4","ids":["C.001","C.002","C.004","C.001.001"]}]
            
      console.log(filterById('C.001.001', users));
      console.log(filterById('132133333', users));

      与lodash/fp 的想法相同:

      const { flow, filter, get, includes, map, cond, isEmpty, constant, stubtrue, identity } = _
      
      const filterById = id => flow(
        filter(flow(get('ids'), includes(id))),
        cond([
          [isEmpty, constant(['missing'])],
          [stubtrue, map('name')]
        ])
      )
      
      const users = [{"name":"missing!","ids":["Not Specified"]},{"name":"user1","ids":["C.001","C.002","C.001.001"]},{"name":"user2","ids":["C.001","C.002","C.004","C.001.001"]},{"name":"user3","ids":["C.001","C.002","C.004","C.001.001"]},{"name":"user4","ids":["C.001","C.002","C.004","C.001.001"]}]
            
      console.log(filterById('C.001.001')(users));
      console.log(filterById('132133333')(users));
      <script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

      【讨论】:

      • 我使用了 @crashmstr 的响应并将其与您对 elvis 运算符的使用结合起来,因为它似乎是“更清洁”的方法。非常感谢:)
      猜你喜欢
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多