lodash的使用

251 阅读1分钟

lodash入门

看起来很费劲的官网文档

学习lodash——这一篇就够用

用一个记录一个

mapValues、keyBy

Lodash _.mapValues()用法及代码示例

在ts里使用:

import _ from 'lodash'

const res = [
    {
      dataIndex"login"
      itemValue: ['IDCard''socialCard']
      ...
    },
    {
      dataIndex"isShowHome"
      itemValue"yes"
      ...
    },
]
console.log(_.keyBy(res, 'dataIndex'))
/*
{
    login: {
          dataIndex: "login"
          itemValue: ['IDCard', 'socialCard']
          ...
    }
    isShowHome: {
          dataIndex: "isShowHome"
          itemValue: "yes"
          ...
    }
}
*/
const resObj = _.mapValues(_.keyBy(res, 'dataIndex'), 'itemValue')
console.log(resObj)
/*
{
    login: ['IDCard', 'socialCard']
    isShowHome: "yes"
}
*/

优化:

import { mapValues, keyBy } from 'lodash'
const resObj = mapValues(keyBy(data, 'dataIndex'), 'itemValue')
const { login, } = resObj

console.log(login)