一道工作中经常遇到的数据转换算法题

问题描述

将原数据类型转换为目标数据类型。
原数据类型如下:

const list1 = [
    {
        typeId: 1,
        off: '111,222',
        on: '333,444',
    },
    {
        typeId: 2,
        off: '111,222',
        on: '333,444',
    },
    {
        typeId: 3,
        off: '111,222',
        on: '333,444',
    },
    {
        typeId: 3,
        off: '111,222',
        on: '333,444',
    }, {
        typeId: 2,
        off: '111,222',
        on: '333,444',
    },
    {
        typeId: 1,
        off: '111,222',
        on: '333,444',
    },
];

目标数据类型:

const result1 = [
    {
        typeId: 1,
        offs: '111,222;111,222',
        ons: '333,444;333,444',
    },
    {
        typeId: 2,
        offs: '111,222;111,222',
        ons: '333,444;333,444',
    },
    {
        typeId: 3,
        offs: '111,222;111,222',
        ons: '333,444;333,444',
    },
];

解决思路

从问题要求不难发现,我们需要做的任务就是将数组中相同typeId的object进行合并,最终返回一个新object数组。
一种比较直观的解决方案是:首先确定原数组中所有的typeId,然后依据每个typeId过滤到原数组中的一组数据,进而将得到的数据进行合并操作,最后将其添入结果数组中即可。
Talk is cheap, show you my code:

function convert1(list1) {
    const set = new Set();
    list1.forEach(list => set.add(list.typeId));
    const result = [];
    set.forEach(item => {
        const targetList = list1.filter(list => list.typeId === item);
        const obj = {
            typeId: item,
            offs: '',
            ons: '',
        };
        targetList.forEach(list => {
            obj.offs = `${obj.offs};${list.off}`;
            obj.ons = `${obj.ons};${list.on}`;
        });
        result.push(obj);
    });
    return result;
}

课后练习

是不是感觉不过瘾,正好我这还有一道和示例题异曲同工的题目。
原数据类型:

const list2 = [
    {
        month: 1,
        type: 'A',
        value: '123',
    },
    {
        month: 2,
        type: 'A',
        value: '123',
    },
    {
        month: 3,
        type: 'A',
        value: '123',
    },
    {
        month: 1,
        type: 'B',
        value: '456',
    },
    {
        month: 2,
        type: 'B',
        value: '456',
    },
    {
        month: 3,
        type: 'B',
        value: '456',
    },
    {
        month: 1,
        type: 'C',
        value: '789',
    },
    {
        month: 2,
        type: 'C',
        value: '789',
    },
    {
        month: 3,
        type: 'C',
        value: '789',
    },
];

目标数据类型:

const result2 = [
    {
        month: 1,
        A: '123',
        B: '456',
        C: '789',
    },
    {
        month: 2,
        A: '123',
        B: '456',
        C: '789',
    },
    {
        month: 3,
        A: '123',
        B: '456',
        C: '789',
    },
];

习题答案

这道题的解题思路与示例题几乎如出一辙,所以这里我就不做思路讲解,直接上答案:

function convert2(list2) {
    const set = new Set();
    list2.forEach(list => set.add(list.month));
    const result = [];
    set.forEach(item => {
        const targetList = list2.filter(list => list.month === item);
        const obj = {
            month: item,
        };
        targetList.forEach(list => {
            obj[list.type] = list.value;
        });
        result.push(obj);
    });
    return result;
}

结语

细心的大佬应该发现,虽然我的解题思路很清晰也很简洁,但是其效率却不是很高,其时间复杂度为O(mn)。(n为原数组的长度,m为目标数组的长度)如果有大佬有更高效的解法,欢迎评论区留言。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值