js中对象属性值相同的合并处理

场景

因为业务需求,某项服务包含不同的地域,为实现自动化处理,需要将不同地域的数据传给处理端,类似于以下这种:

var requestParams = {
    'north-1': [
        {
            cloudServiceType: 'a',resourceType: 'b'
        },
        {
            cloudServiceType: 'c',resourceType: 'd'
        },
        {
            cloudServiceType: 'c',resourceType: 'e'
        },
        {
            cloudServiceType: 'f',resourceType: 'g'
        }
    ],
    'north-4': [
        {
            cloudServiceType: 'a',resourceType: 'b'
        },
        {
            cloudServiceType: 'c',resourceType: 'd'
        },
        {
            cloudServiceType: 'c',resourceType: 'e'
        },
        {
            cloudServiceType: 'f',resourceType: 'g'
        }
    ],
    'east-2': [
        {
            cloudServiceType: 'a',resourceType: 'b'
        },
        {
            cloudServiceType: 'c',resourceType: 'd'
        },
        {
            cloudServiceType: 'c',resourceType: 'e'
        },
        {
            cloudServiceType: 'f',resourceType: 'g'
        }
    ],
    'southwest-2': [
        {
            cloudServiceType: 'a',resourceType: 'b'
        },
        {
            cloudServiceType: 'c',resourceType: 'd'
        }
    ],
    'ap-southeast-1': [
        {
            cloudServiceType: 'a',resourceType: 'b'
        },
        {
            cloudServiceType: 'c',resourceType: 'd'
        }
    ],
}

观察对象不难发现,很多属性对应的值是相同的,为了使处理端减少对象的遍历,我们期望把相对属性值合并,对应属性用类似于‘location1,location2,…’的形式整合。

解决

function mergeObjProperties(requestParams) {
    //用于保存最后格式化的对象
    var formatRequestParams = {};
    //通过keys去对应属性值做比较
    var keys = Object.keys(requestParams);
    //不污染原对象的keys所以另外声明一个用于保存并且初始化第一个地域
    var formatKeys = [];
    formatKeys.push(keys[0]);

    var currentKey = 0;
    formatRequestParams[keys[0]] = requestParams[keys[0]];
    //遍历原对象去和新对象属性值一一比较,相同则更新新对象属性和其对应值,不同则添加新的属性即对应值
    for (var i = 1; i < keys.length; i++) {

        if (equal(formatRequestParams[formatKeys[currentKey]], requestParams[keys[i]])) {
            formatRequestParams[formatKeys[currentKey] + ',' + keys[i]] = requestParams[keys[i]];
            //删除新对象原有属性值
            delete formatRequestParams[formatKeys[currentKey]];
            //更新新对象的属性值
            formatKeys[currentKey] = formatKeys[currentKey] + ',' + keys[i]

        }
        else {
            currentKey++;
            formatKeys.push(keys[i]);
            formatRequestParams[formatKeys[currentKey]] = requestParams[keys[i]];
        }
    }

    return formatRequestParams;
}


function equal(a, b) {

    // 判断数组的长度
    if (a.length !== b.length) {
        return false
    } else {
        // 循环遍历数组的值进行比较
        for (let i = 0; i < a.length; i++) {
            if (a[i].toString() !== b[i].toString()) {
                return false
            }
        }
        return true;
    }
}

最终结果:

{ 
    'north-1,north-4,east-2':
    [ 
        { cloudServiceType: 'a', resourceType: 'b' },
        { cloudServiceType: 'c', resourceType: 'd' },
        { cloudServiceType: 'c', resourceType: 'e' },
        { cloudServiceType: 'f', resourceType: 'g' } 
    ],
    'southwest-2,ap-southeast-1':
    [ 
        { cloudServiceType: 'a', resourceType: 'b' },
        { cloudServiceType: 'c', resourceType: 'd' } 
    ] 
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值