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])
    }
    })
    })

相关推荐
Mh8 小时前
别再只会 `find` 了:Map 在前端业务里的真实用法
前端·javascript
陈随易8 小时前
FFmpeg 9.0 发布,代号 Lei,音视频处理再升级
前端·后端·程序员
爱丶狸9 小时前
Grafana_Zabbix_ImageRenderer_部署与前端操作手册
linux·前端·zabbix·grafana·kylin
用户0595401744612 小时前
把AI对话记忆存储测试从手工改成Playwright+pytest,覆盖率从20%提到96%,回归时间缩短90%
前端·css
kyriewen12 小时前
别再这样写TypeScript了——Code Review中最常见的8个反模式
前端·javascript·typescript
名字还没想好☜12 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
午安~婉12 小时前
GitHub Token/ GitHub Stats统计显示图异常
前端·github·vercel·github token
码云之上12 小时前
AI Agent 工程化总览篇:从 Prompt 到 Harness
前端·人工智能
2501_9269783312 小时前
以说明书 DNA 为模板——完整 AGI 的结构图景
前端·人工智能·经验分享·笔记·ai写作
IT_陈寒13 小时前
Vite静态资源引用这个坑我踩得有点疼
前端·人工智能·后端