第 55 题:某公司 1 到 12 月份的销售额存在一个对象里面

发布于 2022-12-20 12:09:46 字数 541 浏览 137 评论 32

let obj = {1:222, 2:123, 5:888};
obj.length = 12;
let _obj = Array.from(obj).slice(1);
let newObj =  _obj.map((item) => {if(item === undefined) {return null;} else {return item;}});
console.log(newObj);
let obj = {1:222, 2:123, 5:888};
obj.length = 12;
obj[Symbol.iterator] = Array.prototype[Symbol.iterator];
let _obj = [...obj].slice(1);
let newObj =  _obj.map((item) => {if(item === undefined) {return null;} else {return item;}});
console.log(newObj);

思考:

let _obj = [].slice.call(obj);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(32

离鸿 2022-05-04 13:56:42
let obj = {1:222, 2:123, 5:888}
let arr = Array.from({length:12},(v,i) => obj[i+1] || null )
瑾夏年华 2022-05-04 13:56:42
 let obj = {1:222,2:123,5:888};
 let arr = Array.from({length:12}).map((item,i) => obj[i+1]||'null');
 console.log(arr);
//  222,123,null,null,888,null,null,null,null,null,null,null

Array.from({length:12})创建一个12个空位的数组
.map()将obj中的每一项赋值给数组

紅太極 2022-05-04 13:56:42

Array.from({1: 222, 2: 123, 5: 888, length: 13}).slice(1).map((v, i) => v || null)

ま柒月。 2022-05-04 13:56:42

没用es6数组好low

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

function convert(param) {
  if (typeof param === 'object' && param) {
    let arr = new Array(13);
    for (let index = 0; index < arr.length; index++) {
      let e = param[key] : null;
      arr[index] = e;
    }
    return arr.slice(1);
  } else {
    console.warn('输入不合理!');
    return;
  }
}

const obj = { 1: 222, 2: 123, 5: 888 };
let convertArr = convert(obj);
console.log(convertArr);
暮光沉寂i 2022-05-04 13:56:42
let data = {1:222, 2:123, 5: 888}

let r = Array(12).fill(null)
r.forEach((v, i, arr) => {
    data[i] && (arr[i - 1] = data[i])
})
console.log(r)
物价感观 2022-05-04 13:56:42
   const obj = { 1: 222, 2: 123, 5: 888 }
    const vali = (options) => {
        if (Object.prototype.toString.call(options) !== '[object Object]') {
            console.log('请传入对象')
            return false
        }
        return true
    }
    const getSale = (options) => {
        if (!vali(options)) {
            return
        }
        let arr = new Array(13)
        for (let i = 0; i < arr.length; i++) {
            arr[i] = null
            Object.keys(options).forEach(it => {
                if (parseInt(it) === i) {
                    arr[i] = options[it]
                }
            })
        }
        arr.shift()
        return arr
    }
    console.log(getSale(obj))

    // ES6
    const objToArr = (options) => {
        if (!vali(options)) {
            return
        }
        options = { ...options, length: 13 }
        let arr = Array.from(options);
        arr.shift()
        arr = arr.map(item => {
            if (item === undefined) {
                return item = null
            } else {
                return item
            }
        })
        return arr
    }
    console.log(objToArr(obj))

写的好low

任谁 2022-05-04 13:56:42
    let result = Object.entries({ 1: 222, 2: 123, 5: 888 }).reduce(
        (prev, [key, value]) => { prev[key - 1] = value; return prev; },
        Array.from( { length: 12 }, (_) => null )
    );
    console.log(result);
顾挽 2022-05-04 13:56:42
  Object.assign(Array(13).fill(null),{1:222, 2:123, 5:888}).slice(1)
魔法唧唧 2022-05-04 13:56:42

function newData(obj){
obj.length = 12
let newObj = Array.from(obj).map((item,index) =>obj[index+1] || null)
return newObj;
}
console.log(newData({1:222, 2:123, 5:888}))

我要还你自由 2022-05-04 13:56:42

var format = function(sales) {
var arr = new Array(12).fill(null);
for (const month in sales) arr[month - 1] = sales[month];
return arr;
}

又爬满兰若 2022-05-04 13:56:42

function objectToArray(object) {
var arr=new Array(12).fill(null)
var newArr=arr.map((x,index) => {
if(object[index+1]){
return object[index+1]
}else{
return null
}
})
return newArr
}

回答好踊跃,贴上自己的,其实使用的方法在上边都已经有了。感觉不同的方法的区别就是会使用到不同数量的新生成数组,另外代码量也不同,可以根据个人口味选择。

const DATA = {1: 500, 3: 800, 5: 2000};
// const TARGET = [500, null, 800, null, 2000, null, null, null, null, null, null, null];

/** create three arrays */
const transformObjToArrayES6SliceMap = (obj) => Array.from(Object.assign({}, obj, { length: 13 })).slice(1).map((item) => item || null);

/** create one array */
const transformObjToArrayES6ForEach = (obj) => { 
       const arr = Array.from({ length: 12 }); 
       arr.forEach((item, index, arr) => { arr[index] = obj[index+1] || null;}); 
       return arr;
};

/** create two arrays */
const transformObjToArrayES6Map = (obj) => Array.from({ length: 12 }).map((item, index) => obj[index+1] || null);

/** create one array */
const transformObjToArrayFor = (obj) => {
	let arr = [];
	for (let i=0; i < 12; i++) {
		arr[i] = obj[i+1] || null;
	}
	return arr;
};
小糖芽 2022-05-04 13:56:42

改了几次都没有缩进,才意识到这是 markdown

const data = { 1: 222, 2: 123, 5: 888 };

Array(12)
  .fill(null)
  .map((_, index) => {
    const idx = index + 1;
    if (data[idx] !== undefined) return data[idx];
    return null;
  });
饮惑 2022-05-04 13:56:42

大概看了一下,大家的答案都差不多,个人非常喜欢简洁的代码

哎呦我呸! 2022-05-04 13:56:39

const obj = {1:222, 2:123, 5:888};
const result = Array.from({length:12},(item,index)=>(obj[index+1]?obj[index+1]:null));
console.log(result)

破晓 2022-05-04 13:56:39

function formatData (data) {
let result = []
for (var i = 0; i < 12; i++) {
result[i] = null
}
let keys = Object.keys(data)
keys.forEach(o => {
result[parseInt(o) - 1] = data[o]
})

return result
}

var data = { 1: 222, 2: 123, 5: 888 }
formatData(data)

べ繥欢鉨o。 2022-05-04 13:56:30
for循环是最快的。。
var obj = {1:222, 2:123, 5:888}
function hander(obj){
    let arr = [];
  console.time()
    for (let i = 1;i<=12;i++) {
        if(obj[i]){
            arr.push(obj[i])
        }else{
            arr.push(null)
        }
    }
    console.log(arr) // [222, 123, null, null, 888, null, null, null, null, null, null, null]
    console.timeEnd() //default: 0.4609375ms
}
鹿港小镇 2022-05-04 13:56:27

let obj=Object.assign({1:null,2:null,3:null,4:null,5:null,6:null,7:null,8:null,9:null,10:null,11:null,12:null},{1:222,2:123,5:888})
attr=Object.values(obj)
console.log(attr);

不念旧人 2022-05-04 13:56:21
const obj = {1:222, 2:123, 5:888}
const arr = Array.from({length: 12}, (_, index) => (index + 1) in obj ? obj[index + 1] : null)
console.log(arr)
GRAY°灰色天空 2022-05-04 13:56:08
let data = { 1:222, 2:123, 5:888 }
const arr = new Array(12).fill(null).map((item, i) => data[i + 1] || item)
console.log('arr---------', arr)
攒一口袋星星 2022-05-04 13:55:00
var obj = {1:222, 2:123, 5:888};
obj.length = 13;
Array.from(obj,it=>it||null).slice(1);
纸短情长 2022-05-04 13:47:51

var obj = { 1: 222, 2: 123, 5: 888 };
let arr = [];
for(var i = 1; i <= 12; i++) {
if(obj[i]) {
arr.push(obj[i])
} else {
arr.push(null)
}
}
console.log(arr) //[222, 123, null, null, 888, null, null, null, null, null, null, null]

合约呢 2022-05-04 13:46:55

Array.from(Object.assign(obj,{length:12}),v=>v===undefined?null:v)

暗喜 2022-05-04 13:39:04

// 1-12月份的销售额;
var obj = {
1: 222,
2: 123,
5: 888,
}
var arr = new Array(12).fill(null);
var finalArr = arr.map((v, i, curr) => {
if (obj[i + 1]) {
return obj[i + 1];
} else {
return null;
}
});
console.log('--log--:', finalArr)

孤独患者 2022-05-04 13:30:41

var obj = {1:222, 2:123, 5:888}

var c = new Array(12).fill(true).reduce((prev, cur, key) => {
prev[key] = obj[key + 1] || null
return prev
}, [])

空名 2022-05-04 12:38:34

function arrToObject(obj) {
let arr = [];
for (let i = 0; i < 12; i++) {
arr.push(null)
for (let key in obj) {
if(key == (i+1)){
arr.splice(i, 1, obj[key])
}
}
}
return arr;
};
let obj = {1:222, 2:123, 5:888};

怎么感觉我这好low啊!!

凉宸 2022-05-04 04:38:44
let obj = {1:222, 2:123, 5:888};
Array.from({length:12},(item,index)=>obj[index+1]||null)
梨涡 2022-05-03 21:48:03
Array.from({length:12},(v,i)=>({1:222,2:123,5:888}[i+1])||null)
温暖的光 2022-05-02 16:29:24
console.time();
let data = {1:222, 2:123, 5:888};
let arr = Array.from({length:12}).map((it,i)=> data[i+1]||null);
console.log(arr);
// default: 2.119873046875ms
console.timeEnd();

console.time()
let obj = {1:222, 2:123, 5:888};
let res = Array.from({length:12}).fill(null);
keys(obj).forEach(it=>res[it-1] = obj[it]);
console.log(arr);
// default: 0.7451171875ms
console.timeEnd();
帅冕 2022-05-02 12:29:04
let data = {1:222, 2:123, 5:888};
let arr = Array.from({length:12}).fill(null);
keys(data).forEach(it=>arr[it-1] = data[it]);
console.log(arr);
蹲墙角沉默 2022-05-02 08:06:46
let data = {1:222, 2:123, 5:888};
let arr = Array.from({length:12}).map((it,i)=> data[i+1]||null);
console.log(arr)
破晓 2022-05-01 08:12:53
let obj = {1:222, 2:123, 5:888};
const result = Array.from({ length: 12 }).map((_, index) => obj[index + 1] || null);
console.log(result)
~没有更多了~

关于作者

稚气少女

暂无简介

0 文章
0 评论
22 人气
更多

推荐作者

留蓝

文章 0 评论 0

18790681156

文章 0 评论 0

zach7772

文章 0 评论 0

Wini

文章 0 评论 0

ayeshaaroy

文章 0 评论 0

初雪

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文