这道笔试题你会吗(10)

第 55 题:某公司 1 到 12 月份的销售额存在一个对象里面,如下:{1:222, 2:123, 5:888},请把数据处理为如下结构:[222, 123, null, null, 888, null, null, null, null, null, null, null]。

Array.from

语法

Array.from(arrayLike[, mapFn[, thisArg]])

参数

arrayLike:想要转换成数组的伪数组对象或可迭代对象
mapFn (可选参数):如果指定了该参数,新数组中的每个元素会执行该回调函数。
thisArg (可选参数):可选参数,执行回调函数 mapFnthis 对象。

返回值

一个新的数组实例

答案:

let obj = {1:222, 2:123, 5:888};
const result = Array.from({ length: 12 }).map((_, index) => obj[index + 1] || null);
console.log(result)
Array.from({length:12},(v,i)=>({1:222,2:123,5:888}[i+1])||null)

作者:木易杨
博客:https://github.com/yygmind/blog
求点赞 求关注~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容