How to simplify next code:
rules = rules.map(x => Object.assign(x, x.ruleOption.options))
rules.forEach(x => delete x.ruleOption)
rules = _.keyBy(rules, 'code')
I start active use the Lodash, and try to understand right using-way.
You can use destructuring with the rest syntax to get ruleOption
, and create a new object with ruleOption
. You can then use object spread to combine ruleOptions.options
with the rest of the object:
const rules = [{ code: 'x', ruleOption: { options: { type: 'a' }}}, { code: 'y', ruleOption: { options: { type: 'b' }}}]; const newRules = _.keyBy( rules.map(({ ruleOption, ...obj }) => ({ ...obj, ...ruleOption.options })), 'code' ) console.log(newRules)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.