美文网首页让前端飞
这道笔试题你会吗(10)

这道笔试题你会吗(10)

作者: Vicky丶Amor | 来源:发表于2019-04-17 11:38 被阅读1次

第 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

相关文章

网友评论

    本文标题:这道笔试题你会吗(10)

    本文链接:https://www.haomeiwen.com/subject/lcbgwqtx.html