欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2019阿里前端面试题

程序员文章站 2022-03-04 12:15:03
...

2019阿里前端面试题
题目:筛选笔试题 (请在半小时内完成):
var arr = [ {name:‘小米1’, value: 1, type: 2, date: ‘2018-06-07T08:00:01.589Z’ }, {name:‘锤子T1’, value: 1, type: 2, date: ‘2018-06-07T08:10:01.589Z’ }, {name:‘小米2’, value: 1, type: 4, date: ‘2018-06-07T20:00:01.589Z’ }, {name:‘小米2’, value: 4, type: 4, date: ‘2018-06-07T20:10:21.189Z’ }, {name:‘小米4’, value: 1, type: 4, date: ‘2018-06-07T08:00:01.560Z’ }, {name:‘小米4’, value: 2, type: 4, date: ‘2018-06-07T08:10:31.584Z’ }, {name:‘小米6’, value: 1, type: 3, date: ‘2018-06-07T08:00:01.589Z’ }, {name:‘小米5s’,value: 1, type: 4, date: ‘2018-06-07T08:00:01.589Z’ }, {name:‘锤子T2’, value: 1, type: 4, date: ‘2018-06-07T08:00:01.589Z’ }, {name:‘锤子T1’, value: 4, type: 4, date: ‘2018-06-07T08:06:01.589Z’ }, {name:‘魅蓝note5’, value: 1, type: 4, date: ‘2018-06-07T08:00:01.589Z’ }, {name:‘魅蓝note2’, value: 5, type: 4, date: ‘2018-06-02T08:07:01.589Z’ }, {name:‘魅蓝note2’, value: 6, type: 4, date: ‘2018-06-07T08:00:01.589Z’ }, {name:‘魅蓝note3’, value: 1, type: 4, date: ‘2018-06-05T08:00:01.589Z’ }, {name:‘魅蓝note’, value: 1, type: 4, date: ‘2018-06-07T08:00:01.589Z’ }, {name:‘oppor9’, value: 7, type: 4, date: ‘2018-06-04T08:04:01.588Z’ }, {name:‘华为p9’, value: 1, type: 4, date: ‘2018-06-02T08:00:01.577Z’ }, {name:‘华为p9’, value: 2, type: 4, date: ‘2018-06-07T08:00:01.110Z’ }, {name:‘华为p10’, value: 1, type: 1, date: ‘2018-06-07T08:00:01.534Z’ }];

请用您认为最优化的方式,将arr中的type为4的数据过滤出来,

  • 然后按相同的 name + date(按天)合并value(value累加),
  • 然后按 value 降序(从大到小)排序,
  • 最后每行按照 “name,{name},{本地日期},售出${sum(value)}部” 的格式,如:“小米2,2017年06月08日,售出5部”, 打印(console.log)出来。
  • 可以使用第三方js库,可以使用es6。* 请在半小时内完成。要求最后输出结果样例如下:oppor9,2017年06月04日,售出7部魅蓝note2,2017年06月07日,售出6部魅蓝note2,2017年06月02日,售出5部小米2,2017年06月08日,售出5部…

我的方案如下:

var arr = [   {name:'小米1', value: 1,  type: 2, date: '2018-06-07T08:00:01.589Z' },   {name:'锤子T1', value: 1, type: 2, date: '2018-06-07T08:10:01.589Z' },   {name:'小米2', value: 1, type: 4, date: '2018-06-07T20:00:01.589Z' },   {name:'小米2', value: 4, type: 4, date: '2018-06-07T20:10:21.189Z' },   {name:'小米4', value: 1, type: 4, date: '2018-06-07T08:00:01.560Z' },   {name:'小米4', value: 2, type: 4, date: '2018-06-07T08:10:31.584Z' },   {name:'小米6', value: 1, type: 3, date: '2018-06-07T08:00:01.589Z' },   {name:'小米5s',value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' },   {name:'锤子T2', value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' },   {name:'锤子T1', value: 4, type: 4, date: '2018-06-07T08:06:01.589Z' },   {name:'魅蓝note5', value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' },   {name:'魅蓝note2', value: 5, type: 4, date: '2018-06-02T08:07:01.589Z' },   {name:'魅蓝note2', value: 6, type: 4, date: '2018-06-07T08:00:01.589Z' },   {name:'魅蓝note3', value: 1, type: 4, date: '2018-06-05T08:00:01.589Z' },   {name:'魅蓝note', value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' },   {name:'oppor9', value: 7, type: 4, date: '2018-06-04T08:04:01.588Z' },   {name:'华为p9', value: 1, type: 4, date: '2018-06-02T08:00:01.577Z' },   {name:'华为p9', value: 2, type: 4, date: '2018-06-07T08:00:01.110Z' },   {name:'华为p10', value: 1, type: 1, date: '2018-06-07T08:00:01.534Z' }];
var resArr = arr.filter((item) => item.type === 4);
var m = {};
resArr.forEach((item) => {
    var date = new Date(item.date);
    var fD = new Date(`${date.getFullYear()}/${date.getMonth()+1}/${date.getDay()}`).getTime()
    var key = `${item.name}$${fD}`;
    if(m[key]) {
        m[key] += item.value
    } else {
        m[key] = item.value
    }
})
var nArr = []
Object.entries(m).forEach((item) => {
    var cc = item[0].split('$');
    nArr.push({
        date: cc[1],
        name: cc[0],
        value: item[1]
    })
})
nArr.sort((a, b) => b.value-a.value);

nArr.forEach((item) => {
     var date = new Date(Number(item.date));
    console.log(`${item.name},${date.getFullYear()}年${date.getMonth()+1}月${date.getDay()}日,售出${item.value}部`)
})

不知道有没有更好的解决方案,欢迎交流!