Object.fromEntries()
静态方法将键值对列表转换为一个对象
将 Array 转换成对象:
let arr = [["name","张三"],["age","40"]]
let obj = Object.fromEntries(arr);
console.log(obj);
将 Map 转换成对象:
let map = new Map()
map.set("name","李四")
map.set("age",50)
let person = Object.fromEntries(map)
console.log(person);
对象转换:
需求:计算出键名对应的键值的长度,并返回一个新的对象
let Obj = {
"A":["a1","a2","a3"],
"B":["b1","b2"],
"C":["c1"],
}
let myarr = Object.entries(Obj).map(([key,val])=>{
return [key,val.length]
})
let myobj = Object.fromEntries(myarr)
console.log(myobj);