对键在另一个数组中匹配的值应用函数

Apply function over values whose keys match in another array

我有一个form.value如下

 form.value = {
  "to_date": "2019-03-21T05:00:00.000Z",
  "from_date": "2019-03-13T05:00:00.000Z",
  "is_form": ""
  "errors":""
}

我有一个数组如下

filterArray = [
  "from_date",
  "to_date"
]

我想遍历 form.value 对象键并对过滤器数组中匹配的键的值应用函数 (converFormat()) 如下图

  form.value = {
  "to_date": "2019-03-21T05:00:00.000Z",       // apply a function() over value since key is present in the filterArray
  "from_date": "2019-03-13T05:00:00.000Z",     // apply a function() over value since key is present in the filterArray
  "is_form": ""                               
  "errors":""                                  
}
Object.keys(form.value).filter(key => filterArray.includes(key)).forEach(key => {
  form.value[key] = myFunction(form.value[key])
})

// Or if you want to cache the value in some other variable

const customFormValue = {
  ...form.value,
  ...Object.keys(form.value)
    .filter(key => filterArray.includes(key))
    .reduce(
      (pr, curr) => ({
        ...pr,
        [curr]: myFunction(form.value[curr])
      }),
      {}
    )
};