Javascript 如何使用lodash生成密钥配对对象结果?
我有以下数组:Javascript 如何使用lodash生成密钥配对对象结果?,javascript,arrays,object,functional-programming,lodash,Javascript,Arrays,Object,Functional Programming,Lodash,我有以下数组: const ids = ["1234", "5678", "0987", "6543"] 我需要一个包含lodash的函数,它返回: const result = {"1234": { workId: null }, "5678": { workId: null }, "0987": { workId: null }, "6543": { workId: null }} 使用lodash方法的方式是什么 感谢您的帮助免责声明:lodash在这方面做得太过分了 你可以用。。。或
const ids = ["1234", "5678", "0987", "6543"]
我需要一个包含lodash的函数,它返回:
const result = {"1234": { workId: null }, "5678": { workId: null }, "0987": { workId: null }, "6543": { workId: null }}
使用lodash方法的方式是什么
感谢您的帮助免责声明:
lodash
在这方面做得太过分了
你可以用。。。或者它的
const id=[“1234”、“5678”、“0987”、“6543”]
log(ids.reduce((acc,key)=>Object.assign(acc,{[key]:{workId:null}}),{})代码>免责声明:lodash
在这方面做得太过分了
你可以用。。。或者它的
const id=[“1234”、“5678”、“0987”、“6543”]
log(ids.reduce((acc,key)=>Object.assign(acc,{[key]:{workId:null}}),{})代码>这是一个使用和
const id=[“1234”、“5678”、“0987”、“6543”];
const result=(id)
.invert()
.mapValues(()=>({workId:null}))
.value();
控制台日志(结果)代码>
。作为控制台包装{最小高度:100%;顶部:0}
这里有一个lodash解决方案,它使用
const id=[“1234”、“5678”、“0987”、“6543”];
const result=(id)
.invert()
.mapValues(()=>({workId:null}))
.value();
控制台日志(结果)代码>
。作为控制台包装{最小高度:100%;顶部:0}
为什么需要使用lodash?使用普通的forEach
constresult={},您可以轻松地做到这一点;对于(id的常量id){result[id]={workId:null};}
ids.reduce((a,k)=>({…a[k]:{workId:null}),{})
?为什么需要使用lodash?使用普通的forEach
constresult={},您可以轻松地做到这一点;对于(id的常量id){result[id]={workId:null};}
ids.reduce((a,k)=>({…a[k]:{workId:null}),{})
???
const result = _(ids)
.invert()
.mapValues(() => ({ workId: null }))
.value();