【问题标题】:How to alter keys in immutable map?如何更改不可变映射中的键?
【发布时间】:2017-10-25 01:04:44
【问题描述】:

我有这样的数据结构(由 normalizr 生成):

const data = fromJS({
  templates: {
    "83E51B08-5F55-4FA2-A2A0-99744AE7AAD3":
      {"uuid": "83E51B08-5F55-4FA2-A2A0-99744AE7AAD3", test: "bla"},
    "F16FB07B-EF7C-440C-9C21-F331FCA93439":
      {"uuid": "F16FB07B-EF7C-440C-9C21-F331FCA93439", test: "bla"}
  }
})

现在我尝试弄清楚如何替换模板条目的键和值中的 UUID。基本上我怎样才能归档以下输出:

const data = fromJS({
  templates: {
    "DBB0B4B0-565A-4066-88D3-3284803E0FD2":
      {"uuid": "DBB0B4B0-565A-4066-88D3-3284803E0FD2", test: "bla"},
    "D44FA349-048E-4006-A545-DBF49B1FA5AF":
      {"uuid": "D44FA349-048E-4006-A545-DBF49B1FA5AF", test: "bla"}
  }
})

在我看来,.mapEntries() 方法是一个不错的候选者,但我正在为如何使用它而苦苦挣扎......

// this don't work ... :-(
const result = data.mapEntries((k, v) => {
  const newUUID = uuid.v4()
  return (newUUID, v.set('uuid', newUUID))
})

也许有人可以帮我一把?

【问题讨论】:

    标签: immutable.js normalizr


    【解决方案1】:

    mapEntries 是正确的方法。从documentation 开始,映射函数具有以下签名:

    mapper: (entry: [K, V], index: number, iter: this) => [KM, VM]
    

    这意味着第一个参数是作为 [key, value] 数组传入的条目。同样,mapper 函数的返回值应该是新键和新值的数组。因此,您的映射器函数需要如下所示:

    ([k, v]) => {
      const newUUID = uuid.v4()
      return [newUUID, v.set('uuid', newUUID)]
    } 
    

    这等价于以下(更明确的)函数:

    (entry) => {
      const key = entry[0]; // note that key isn't actually used, so this isn't necessary
      const value = entry[1];
      const newUUID = uuid.v4()
      return [newUUID, value.set('uuid', newUUID)]
    }
    

    需要注意的一点是,模板嵌套在templates 属性下,因此您不能直接映射数据——相反,您需要使用update 函数。

    data.update('templates', templates => template.mapEntries(...)))
    

    因此,将所有内容放在一起,您的解决方案应如下所示:

    const result = data.update('templates', templates => 
      templates.mapEntries(([k, v]) => {
        const newUUID = uuid.v4()
        return [newUUID, v.set('uuid', newUUID)]
      })
    );
    

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      相关资源
      最近更新 更多