简体   繁体   中英

How to perform subtraction on object's data in Node.JS?

I've the data of a censor. It consists of current and previous month's data as an array of objects. Now I need to find the difference of current and previous month's data and assign the difference to the previous month's data object.

For example, I've the following data.

deviceData = [
        {
            "_id": "sb-0001",
            "heat": 100,
            "humidity": 200,
            "time": "This Month"
        },
        {
            "_id": "a-1",
            "heat": 60,
            "humidity": 40,
            "time": "This Month"
        },
        {
            "_id": "a-1",
            "heat": 20,
            "humidity": 10,
            "time": "Last Month"
        },
        {
            "_id": "sb-0001",
            "heat": 20,
            "humidity": 30,
            "time": "Last Month"
        }
    ]

Now I want to find the difference like the following.

deviceData = [
        {
            "_id": "sb-0001",
            "heat": 100,
            "humidity": 200,
            "time": "This Month"
        },
        {
            "_id": "a-1",
            "heat": 60,
            "humidity": 40,
            "time": "This Month"
        },
        {
            "_id": "a-1",
            "heat": 20,
            "humidity": 10,
            "time": "Last Month",
            "diff_heat":40,
            "diff_humidity":30
        },
        {
            "_id": "sb-0001",
            "heat": 20,
            "humidity": 30,
            "time": "Last Month",
            "diff_heat":80,
            "diff_humidity":170
        }
    ]

I've tried the following code to calculate the difference but didn't get that.

let difference;
        difference = [templateData.reduce((obj, n) => {  
            for (var prop in n) {
                if (obj.hasOwnProperty(prop) && obj['time']==n['time']) obj[prop] -= n[prop];
                else obj[prop] = n[prop];
            }
            return obj;
        }, {})]

Is there any other way to do that?

This code should do what you wish, we create a map of last month's data, then loop through the array and subtract from the current month.

I've made this a little more generic by looping over properties of the objects and subtracting if they are numbers.

 const deviceData = [ { "_id": "sb-0001", "heat": 100, "humidity": 200, "time": "This Month" }, { "_id": "a-1", "heat": 60, "humidity": 40, "time": "This Month" }, { "_id": "a-1", "heat": 20, "humidity": 10, "time": "Last Month" }, { "_id": "sb-0001", "heat": 20, "humidity": 30, "time": "Last Month" } ]; let lastMonthData = deviceData.reduce((acc, el) => { if (el.time === "Last Month") { acc[el._id] = el; } return acc; }, {}); let result = deviceData.reduce((acc, el) => { if (el.time === "This Month" && lastMonthData[el._id]) { Object.entries(el).forEach(([key,val]) => { if (Number.isFinite(lastMonthData[el._id][key])) { lastMonthData[el._id][key + "_diff"] = val - lastMonthData[el._id][key]; } }); } acc.push(el); return acc; }, []); console.log("Result:", result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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