今天小编要更新的是es10对对象的扩展,是Object下的一个静态方法,Object.fromEntries(),这个方法是和前面的Object.entries()逆向的两个交互方法手术。之前我们在对对象进行扩展的时候,就有这样一个方法,我们可以这样使用。也可以关注我的微信公众号,蜗牛全栈。constobj={name:"lilei",age:12}constentries=Object.entries(obj)console.log(entries)//[["name","lilei"],["age",12]]通过今天对象的扩展方法,我们可以使用constentries=[["name","lilei"],["age",12]]constfromentries=Object.fromEntries(entries)console.log(fromentries)//{name:"lilei",age:12}对于这个新的扩展,下面列出了一些实际的应用场景1.将map转换为对象constmap=newMap()map.set("name","lilei")map.set("age",18)console.log(map)//Map(2){name:"lilei",age:18}constfromentries=Object.fromEntries(map)console.log(fromentries)//{name:"lilei",age:18}2.根据指定条件,过滤对象中的属性和值//获取对象中大于80分的课程和分数constcourse={math:12,English:90,Chinese:87}//数组方法比较多,先把对象转成数组constres=Object.entries(course).filter(([key,value])=>{returnvalue>80})安慰。log(res)//[["English",90],["Chinese",87]]console.log(Object.fromEntries(res))//{English:90,Chinese:87}
