【问题标题】:Ramda.js pluck(key) type vs native mapRamda.js pluck(key) 类型与原生地图
【发布时间】:2017-05-23 11:36:52
【问题描述】:

假设我有以下代码:

const results = //some complex datastructure generated by a third party api call
const reducedList = results.map((item) => item.awesome_key)
  .map((awesomeKeyList) => awesomeKeyList
    .reduce((memo, awesomeKey) => {//stuff},{})))

这段代码就像一个魅力。现在说我决定通过 pluck 将 Ramda 用于第一张地图,如下所示:

  import R from Ramda;
  R.pluck('awesome_key', results)
    .map((awesomeKeyList) => awesomeKeyList
      .reduce((memo, awesomeKey) => {},{})))

这将失败:

Property 'reduce' does not exist on type '{}'.

Ramda.pluck 上的类型有:

pluck<T>(p: string|number, list: any[]): T[];
pluck(p: string|number): <T>(list: any[]) => T[];

那些类型会阻止我以这种方式使用 reduce?

一个示例(简化)结构:

things: [
  {
    awesome_key: [{
      long_name: 'string',
      short_name: 'string',
      types: {
        0: 'string from set',
        1?: 'string'
      }
    }]
    other_fields not relevant here
    }
]

【问题讨论】:

  • 您能否分享一些 (simplified) 数据来证明该问题?
  • 按要求添加来自第三方 API 的数据
  • 无论哪种格式,我都得到相同的结果。因此,您的代码或数据可能有一些特定的东西。
  • 你对比results.map((item) =&gt; item.awesome_key)R.pluck('awesome_key', results)的结果了吗,应该是一样的...

标签: typescript types ramda.js pluck


【解决方案1】:

从这里开始:

const results = [
  {
    awesome_key: [{
      long_name: 'foo',
      short_name: 'bar',
      types: {
        0: 'abc',
        1: 'def',
        2: 'ghi'
      }
    }, {
      long_name: 'baz',
      short_name: 'qux',
      types: {
        0: 'pqr',
        1: 'xyz'
      }
    }],
    other_fields: 'not relevant here'
    }
]
const interestingTypes = ['pqr', 'xyz'];

据我所知,这两者具有相同的行为:

results.map((item) => item.awesome_key)
  .map((addressComponentList) => addressComponentList.reduce((memo, addressComponent) => {
     if (interestingTypes.indexOf(addressComponent.types[0]) !== -1) {
       if (!memo[addressComponent.types[0]]) {
         memo[addressComponent.types[0]] = addressComponent.long_name
       }  
     }
     return memo
   },{}));

R.pluck('awesome_key', results)
  .map((addressComponentList) => addressComponentList.reduce((memo, addressComponent) => {
     if (interestingTypes.indexOf(addressComponent.types[0]) !== -1) {
       if (!memo[addressComponent.types[0]]) {
         memo[addressComponent.types[0]] = addressComponent.long_name
       }  
     }
     return memo
   },{}));

就像这样,这对 Ramda 来说更惯用:

R.pipe(
  R.pluck('awesome_key'),
  R.map(reduce((memo, addressComponent) => {
     if (interestingTypes.indexOf(addressComponent.types[0]) !== -1) {
       if (!memo[addressComponent.types[0]]) {
         memo[addressComponent.types[0]] = addressComponent.long_name
       }  
     }
     return memo
   },{}))
)(results) 

显然这也可以稍微清理一下,但我将代码基于your other question

pluck('field', xs) 确实应该返回与xs.map(x =&gt; x.field) 相同的值。

您列出的打字稿签名不是我对pluck 的看法,它被记录为:: k -&gt; [{k: v}] -&gt; [v],这意味着它接受一个(字符串)键和一个包含该键的对象列表,返回一个值列表

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 2010-09-09
    • 2015-07-13
    • 1970-01-01
    • 2020-03-11
    • 2016-04-06
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多