首页 > 解决方案 > 想要在对象键中的__键之后拆分键并过滤

问题描述

sample = {
  name__subcatogary: 'help',
  name__Angel: 'sample'
}

想要在 __ key in object 之后对对象进行排序或过滤

我使用了排序和过滤器,但我无法先拆分它并且

this.sample = res.msg.recordset[0];
console.log(">>>>>>>>>",this.sample.split('__')[1].filter())

过滤对象

sample = {
  name__Angel: 'help',
  name__subcatogary: 'sample'
}

标签: javascript

解决方案


Object.keys(sample)
    .map(key => ({key: key, fixed: key.split('__')[1], value: sample[key]}))
    .filter(item => filterFunction(item.fixed))
    .reduce((p, c) => (p[c.key] = c.value) && p, {})
  1. 拿钥匙Object.keys
  2. map具有原始键、值和固定键的新对象数组。
  3. filter带有您的 'filterFunction'的数组。
  4. 使用过滤器键创建新对象reduce

推荐阅读