【发布时间】:2018-11-09 20:30:46
【问题描述】:
我有以下数组:
const ids = ["1234", "5678", "0987", "6543"]
我需要一个带有 lodash 的函数来返回:
const result = {"1234": { workId: null }, "5678": { workId: null }, "0987": { workId: null }, "6543": { workId: null }}
lodash 方法的使用方式是什么?
感谢您的帮助
【问题讨论】:
-
为什么需要使用lodash?您可以使用普通的
forEach轻松完成此操作 -
const result = {}; for (const id of ids) { result[id] = {workId: null}; } -
ids.reduce((a, k) => ({...a, [k]: { workId: null } }), {})???
标签: javascript arrays object functional-programming lodash