js 将对象转换为数组,两个对象的属性相同合并相加

1、定义两个对象,属性相同的值相加,并将值赋值为数组中对象属性相同的
  • 定义数组

    var titleDate = [
    {
    name: 'JKJInputQty',
    field: 'restQty',
    icon: config.ossUrl + '/afl-data-screen/images/icon-all-input.png',
    value: 0,
    titleNames: 'JKJInputQty',
    span: 3
    }, {
    name: 'JKJOutputQty',
    field: 'restQty',
    icon: config.ossUrl + '/afl-data-screen/images/icon-takeOut.png',
    value: 0,
    titleNames: 'JKJOutputQty',
    span: 3
    }, {
    name:'PEOutputQty',
    field: 'restQty',
    icon: config.ossUrl + '/afl-data-screen/images/icon-output.png',
    value: 0,
    titleNames: 'PEOutputQty',
    span: 3
    }, {
    name: 'abnormalCause',
    field: 'restQty',
    icon: config.ossUrl + '/afl-data-screen/images/icon-search.png',
    cause: '',
    titleNames: 'AbnormalCause',
    span: 5
    }]

  • 定义两个对象

    var ProductDataByDayofLine1= {
    "JKJInputQty": 3276,
    "JKJOutputQty": 9890,
    "PEOutputQty": 8442,
    "DROutputQty": 10584,
    "JKJOutputDifferenceQty": 6614,
    "PEOutputDifferenceQty": 0,
    "DROutputDifferenceQty": 2142,
    "AbnormalCause": "JKJ产出数量大于投入数量DR产出数量大于PE产出数量"
    }
    var ProductDataByDayofLine2= {
    "JKJInputQty": 0,
    "JKJOutputQty": 0,
    "PEOutputQty": 0,
    "DROutputQty": 0,
    "JKJOutputDifferenceQty": 0,
    "PEOutputDifferenceQty": 0,
    "DROutputDifferenceQty": 0,
    "AbnormalCause": ""
    },

  • 将连个对象合并相加

    const keys = Object.keys(ProductDataByDayofLine1)
    keys.forEach((name, index) => {
    titleDate.forEach(item => {
    if (name === item.titleNames && index < 4) {
    item.value = Number(ProductDataByDayofLine1[name]) + Number(ProductDataByDayofLine2[name])
    }
    if (name === item.titleNames && item.titleNames === 'JKJOutputDifferenceQty') {
    // Number() 将值转换类型为数字类型
    const a = Number(ProductDataByDayofLine1[name]) + Number(ProductDataByDayofLine2[name])
    const b = Number(ProductDataByDayofLine1.PEOutputDifferenceQty) + Number(ProductDataByDayofLine2.PEOutputDifferenceQty)
    const c = Number(ProductDataByDayofLine1.DROutputDifferenceQty) + Number(ProductDataByDayofLine2.DROutputDifferenceQty)
    item.value = a + b + c
    }
    //特定的判断条件去添加到titleDate数组属性中
    if (name === item.titleNames && item.titleNames === 'AbnormalCause') {
    item.cause = res.ProductDataByDayofLine1[name] + ProductDataByDayofLine2[name]
    }
    })
    })

2、将对象转换为数组并合并对象

数据格式为

复制代码
var ProductDataByHourofLine1= {
        "HourOne": {
            "JKJInputQty": 0,
            "JKJOutputQty": 0,
            "PEOutputQty": 0,
            "DROutputQty": 0,
            "JKJOutputDifferenceQty": 0,
            "PEOutputDifferenceQty": 0,
            "DROutputDifferenceQty": 0,
            "AbnormalCause": null
        },
        "HourTwo": {
            "JKJInputQty": 0,
            "JKJOutputQty": 0,
            "PEOutputQty": 0,
            "DROutputQty": 0,
            "JKJOutputDifferenceQty": 0,
            "PEOutputDifferenceQty": 0,
            "DROutputDifferenceQty": 0,
            "AbnormalCause": null
        },
        "HourThree": {
            "JKJInputQty": 0,
            "JKJOutputQty": 0,
            "PEOutputQty": 0,
            "DROutputQty": 0,
            "JKJOutputDifferenceQty": 0,
            "PEOutputDifferenceQty": 0,
            "DROutputDifferenceQty": 0,
            "AbnormalCause": null
        },
        "HourFour": {
            "JKJInputQty": 0,
            "JKJOutputQty": 0,
            "PEOutputQty": 0,
            "DROutputQty": 0,
            "JKJOutputDifferenceQty": 0,
            "PEOutputDifferenceQty": 0,
            "DROutputDifferenceQty": 0,
            "AbnormalCause": null
        },
        
}

var ProductDataByHourofLine2= {
        "HourOne": {
            "JKJInputQty": 0,
            "JKJOutputQty": 0,
            "PEOutputQty": 0,
            "DROutputQty": 0,
            "JKJOutputDifferenceQty": 0,
            "PEOutputDifferenceQty": 0,
            "DROutputDifferenceQty": 0,
            "AbnormalCause": null
        },
        "HourTwo": {
            "JKJInputQty": 0,
            "JKJOutputQty": 0,
            "PEOutputQty": 0,
            "DROutputQty": 0,
            "JKJOutputDifferenceQty": 0,
            "PEOutputDifferenceQty": 0,
            "DROutputDifferenceQty": 0,
            "AbnormalCause": null
        }
      //......后面定义省略
}
  • 转换对象并添加到新的数组中

    let arr=[]
    let arr2=[]
    Object.entries(ProductDataByHourofLine1).forEach((item, index) => {
    if (item[0] === 'HourOne') {
    item[1].TimePeriod = '00:00 ~ 01:00'
    arr.push(item[1])
    }
    if (item[0] === 'HourTwo') {
    item[1].TimePeriod = '01:00 ~ 02:00'
    arr.push(item[1])
    }
    if (item[0] === 'HourThree') {
    item[1].TimePeriod = '02:00 ~ 03:00'
    arr.push(item[1])
    }
    if (item[0] === 'HourFour') {
    item[1].TimePeriod = '03:00 ~ 04:00'
    arr.push(item[1])
    }
    })

    Object.entries(ProductDataByHourofLine2).forEach((item, index) => {
    if (item[0] === 'HourOne') {
    item[1].TimePeriod = '00:00 ~ 01:00'
    arr2.push(item[1])
    }
    if (item[0] === 'HourTwo') {
    item[1].TimePeriod = '01:00 ~ 02:00'
    arr2.push(item[1])
    }
    if (item[0] === 'HourThree') {
    item[1].TimePeriod = '02:00 ~ 03:00'
    arr2.push(item[1])
    }
    if (item[0] === 'HourFour') {
    item[1].TimePeriod = '03:00 ~ 04:00'
    arr2.push(item[1])
    }
    })

  • 将两个数组中对象相同的合并相加 到某一个数组的属性上

    arr.forEach((item, index) => {
    var item2 = arr2[index]
    var a = Object.keys(item)
    var b = Object.keys(item2)
    a.forEach((x, index) => {
    if (x === b[index] && x !== 'TimePeriod') {
    item[x] = Number(item[x]) + Number(item2[x])
    }
    })
    })

相关推荐
yinuo1 小时前
前端跨页面通讯终极指南⑥:SharedWorker 用法全解析
前端
PineappleCoder6 小时前
还在重复下载资源?HTTP 缓存让二次访问 “零请求”,用户体验翻倍
前端·性能优化
拉不动的猪6 小时前
webpack编译中为什么不建议load替换ast中节点删除consolg.log
前端·javascript·webpack
李姆斯6 小时前
Agent时代下,ToB前端的UI和交互会往哪走?
前端·agent·交互设计
源码获取_wx:Fegn08956 小时前
基于springboot + vue健身房管理系统
java·开发语言·前端·vue.js·spring boot·后端·spring
闲谈共视6 小时前
基于去中心化社交与AI智能服务的Web钱包商业开发的可行性
前端·人工智能·去中心化·区块链
CreasyChan6 小时前
C# 反射详解
开发语言·前端·windows·unity·c#·游戏开发
JIngJaneIL7 小时前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
阿蒙Amon8 小时前
JavaScript学习笔记:6.表达式和运算符
javascript·笔记·学习
hashiqimiya8 小时前
两个步骤,打包war,tomcat使用war包
java·服务器·前端