简体   繁体   English

如何使用从 Node.JS 中减去 2 个其他对象获得的数据创建动态 object?

[英]How to create a dynamic object with data obtained from subtraction of 2 other objects in Node.JS?

I've a use case in which I need to generate the object with present and previous month's(or quarter or week) data and also the difference between them too.我有一个用例,我需要用当前和上个月(或季度或周)的数据生成 object 以及它们之间的差异。

I've got the whole data.我有完整的数据。 But I'm confused on how to perform the subtraction among object values and create a new object with it's results.但是我对如何在 object 值之间执行减法并用它的结果创建一个新的 object 感到困惑。

For example, I've the following data.例如,我有以下数据。

data = {
        "variables": [
            "heat",
            "humidity"
        ],
        "lables": [
            "February",
            "January",
            "December",
            "November",
            "October",
            "September"
        ],
        "values": [
            [
                300,
                40,
                0,
                7000,
                250,
                150
            ],
            [
                400,
                10,
                0,
                8000,
                150,
                50
            ]
        ]
}

Now I want to generate the following object.现在我想生成以下 object。

result= {
        "variables": [
            "heat",
            "humidity"
        ],
        "lables": [
            "February",
            "January",
            "December",
            "November",
            "October",
            "September"
        ],
        "values": [
            [
                300,
                40,
                0,
                7000,
                250,
                150
            ],
            [
                400,
                10,
                0,
                8000,
                150,
                50
            ]
        ],
        "current":{
             "_id":"February",
             "heat":300,
             "humidity":400
        },
        "previous":{
             "_id":"January",
             "heat":40,
             "humidity":10
        },
        "difference":{
             "heat":260,
             "humidity":390
        },
}

Here, the lables and variables are completely dynamic.在这里, lablesvariables是完全动态的。 ie, if there is only one variable is available, there will be only one array values array.即,如果只有一个变量可用,则只有一个数组values数组。 Is there any way to achieve the above result?有什么办法可以达到上面的结果吗?

You can try something like this:你可以尝试这样的事情:

data.current = {
    _id: data.lables[0],
    ...(data.variables.reduce((obj, variableName, variableIndex) => {
        obj[variableName] = data.values[variableIndex][0]
        return obj
    }, {})
} 
data.previous = {
    _id: data.lables[1],
    ...(data.variables.reduce((obj, variableName, variableIndex) => {
        obj[variableName] = data.values[variableIndex][1]
        return obj
    }, {}))
}
data.difference = {
  ...(data.variables.reduce((obj, variableName, variableIndex) => {
        obj[variableName] = data.current[variableName] - data.previous[variableName]
        return obj
    }, {}))      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM