用一个记录一个
mapValues、keyBy
在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)