题目:某公司 1 到 12 月份的销售额存在一个对象里面,如下:{1:222, 2:123, 5:888},请把数据处理为如下结构:[222, 123, null, null, 888, null, null, null, null, null, null, null]。
方法一:
const handle1 = obj => {
let array = new Array(12).fill(null);
for (let i = 0; i < 12; i++) {
if (obj[i + 1]) {
array.splice(i, 1, obj[i + 1]);
}
}
return array;
};
方法二:
const handle2 = obj =>Array.from({ length: 12 }, (item, index) => obj[index + 1] || null);
方法三:
const handle3 = obj =>Array.from({ length: 12 }).map((item, index) => obj[index + 1] || null);
方法四:
const handle4 = obj => Object.assign(Array(13).fill(null), obj).slice(1);