-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 55 题:某公司 1 到 12 月份的销售额存在一个对象里面 #96
Comments
let obj = {1:222, 2:123, 5:888};
const result = Array.from({ length: 12 }).map((_, index) => obj[index + 1] || null);
console.log(result) |
let data = {1:222, 2:123, 5:888};
let arr = Array.from({length:12}).map((it,i)=> data[i+1]||null);
console.log(arr) |
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); |
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(); |
Array.from({length:12},(v,i)=>({1:222,2:123,5:888}[i+1])||null) |
let obj = {1:222, 2:123, 5:888};
Array.from({length:12},(item,index)=>obj[index+1]||null) |
function arrToObject(obj) { 怎么感觉我这好low啊!! |
var obj = {1:222, 2:123, 5:888} var c = new Array(12).fill(true).reduce((prev, cur, key) => { |
// 1-12月份的销售额; |
|
var obj = { 1: 222, 2: 123, 5: 888 }; |
var obj = {1:222, 2:123, 5:888};
obj.length = 13;
Array.from(obj,it=>it||null).slice(1); |
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) |
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) |
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}) |
|
const obj = {1:222, 2:123, 5:888}; |
function formatData (data) { return result var data = { 1: 222, 2: 123, 5: 888 } |
let obj = {1:222, 2:123, 5:888}
let arr = Array.from({length:12},(v,i) => obj[i+1] || null ) |
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({1: 222, 2: 123, 5: 888, length: 13}).slice(1).map((v, i) => v || null) |
没用es6数组好low
|
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) |
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 |
|
Object.assign(Array(13).fill(null),{1:222, 2:123, 5:888}).slice(1) |
function newData(obj){ |
var format = function(sales) { |
function objectToArray(object) { |
回答好踊跃,贴上自己的,其实使用的方法在上边都已经有了。感觉不同的方法的区别就是会使用到不同数量的新生成数组,另外代码量也不同,可以根据个人口味选择。
|
|
var a = {1:222, 2:123, 5:888} |
let obj = {1:222, 2:123, 5:888}; |
function dataTransform(object) {
return Array.from(Array(12)).map((item, index) => {
let result = null
Object.keys(object).forEach((key) => {
if (key === String(index + 1)) {
result = object[key]
}
})
return result
})
} |
var arr = new Array(12).fill(null), |
如果其中一个月份销售额为0就不行了 let obj = {1:222, 2:123, 5:888, 4: 0};
const result = Array.from({ length: 12 }).map((_, index) => obj[index + 1] || null);
console.log(result) |
function toRe(obj) {
let re = [];
for (let i = 1; i < 13; i++) {
// 遍历1-12,出现在给定对象中则赋对应值,否则置为null;
re[i - 1] = i in obj ? obj[i] : null;
}
return re;
}
console.log(toRe({ 1: 222, 2: 123, 5: 888 })); |
Object.assign(new Array(13).fill(null),{1:222, 2:123, 5:888}).slice(1) |
const data = {1:222, 2:123, 5:888}
const map = new Array(12).fill(null)
console.log(map.map((item,i)=> data[i+1] || item))
``` |
let data = { 1: 222, 2: 123, 5: 888 };
let arr = new Array(12).fill(null);
Object.keys(data).forEach(month => {
arr[month - 1] = data[month];
});
console.log(arr); |
这个 || 要换成 ?? 吧 |
let obj = {1:222, 2:123, 5:888}; |
const obj = {1:222, 2:123, 5:888} let res = new Array(12).fill(null).map((item,index)=>(item=obj[index+1] || null,item)) console.log(res); |
let obj = {
1:222,
2:123,
5:888
}
let arr = new Array(12).fill(null);
for(let i in obj){
arr.splice(i-1,1,obj[i]);
}
console.log(arr); |
let obj = {
1: 222,
2: 123,
5: 888,
length: 12
}
Array.from(obj).slice(1).map(item => !item ? null : item) |
你好,我已经收到你的来信!
|
let o = { 1: 222, 2: 123, 5: 888 }; |
let target = {1:222, 2:123, 5:888} |
|
你好,我已经收到你的来信!
|
|
export function objectToArr (data: Record<string, number>): Array<any> {
const result = new Array(12).fill(null)
Object.keys(data).forEach((key: string) => result[+key - 1 ] = data[key])
return result
} |
const mount = { |
const transformSale = sales => {
const monthsList = Array.from({length: 12}).map((_, index) => index+1);
return monthsList.map(month => sales[month] ?? null)
}
const monthSalesTarget = {1: 222, 2: 123, 5: 888}
const monthSaleList = transformSale(monthSalesTarget);
console.log(monthSaleList); |
你好,我已经收到你的来信!
|
const base = { 1: 222, 2: 123, 5: 888 }
const newArray2 = Array.from(Array(12), (item, index) => [index + 1, null])
const resObj = { ...Object.fromEntries(newArray2), ...base }
const objValues = Object.values(resObj)
console.log(objValues) |
|
你好,我已经收到你的来信!
|
const data={1:222, 2:123, 5:888}; |
思考:
The text was updated successfully, but these errors were encountered: