Reputation: 2288
I am trying to find the sum of each channel to get the total energy for energy_ac. This is the mapping.
var mappings = [
{
floor: 13,
name: 1301,
room_type: 'room',
energy_ac: [
{deviceId: 15062, channels: ['ct1']},
{deviceId: 15063, channels: ['ct1', 'ct2', 'ct3']}
],
energy_light: [
{deviceId: 15062, channels: ['ct4']}
],
energy_socket1: [
{deviceId: 15062, channels: ['ct5']}
],
energy_socket2: [
{deviceId: 15062, channels: ['ct5']}
]
} ];
This is the data:
data = { '15062':
{ _id: 550fea1b46758f1505dd70c7,
deviceId: '15062',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 },
'15063':
{ _id: 550fea1b46758f1505dd70c8,
deviceId: '15063',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 },
'15064':
{ _id: 550fea1b46758f1505dd70c9,
deviceId: '15064',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 },
'15065':
{ _id: 550fea1b46758f1505dd70ca,
deviceId: '15065',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 } }
This is my code:
mappings.forEach(function(room) {
var hash = {};
hash.floor = room.floor;
hash.name = room.name;
hash.room_type = room.room_type;
hash.energy_ac = 0;
room.energy_ac.forEach(function(device) {
device.channels.forEach(function(channel){
hash.energy_ac += room[device][channel];
});
});
room[device][channel] is not working for me. I also tried room[device].channel . I am not sure how to get the value of channel. Any help would be appreciated.
Upvotes: 0
Views: 59
Reputation: 106
I think the chief problem here is that you're attempting to get the energy from the device
object, when it's actually stored in the data
object. The keys you need to index into the data
object are stored in device
.
Assuming you want the total energy_ac per room, what you need to do is:
for each room:
set hash.energy_ac = 0
for each device in room's energy_ac:
for each channel in this device:
add the energy of this device and channel to hash.energy_ac
So for the one room in mappings
(1301), the energy_ac
should be 75. Its energy_ac
is comprised of device 15062: channel ct1 (34), plus device 15063: channels ct1 (34) + ct2 (0) + ct3 (7). 34 + 34 + 7 = 75.
Here's a JSFiddle with (what I think is) the implementation you intended.
Also, I was getting console errors due to the _id
values not being strings.
Upvotes: 1
Reputation: 1849
Device is nested inside of energy_ac, so you can't access it directly from room. You need to do something like room.energy_ac[device].channels[channel]
. This still isn't going to work though, because channel isn't the index of the array, it's the current item, which is the channel value. What you really need to do is just run hash.energy_ac += channel
, since channel is the number you want to add anyway.
Upvotes: 1