me1 - 7 months ago 32
Node.js Question

How can i calculate each user positive,negative point and remaining point?

Below is my data , which is stored in document .i want to calculate each user positive_point and nagative_point and remaining point .remaining point means positive_point - otal nagative_point

1. 580da5653bd3cc5802f6c37c

2. 3bd3cc5802f6c37c580da565

3. 2f63bc580c37c580da565d3c

my expectation :

``````for examplpe i take 580da5653bd3cc5802f6c37c(user)

total positive point: 150+30+0    = 180
total nagative  point :0+50+150  = 200
remaining point

= -20
``````

like that each user i need to calculate some one help me out

``````[
{ _id: 6d2847090a6a1b,
user_id: '580da5653bd3cc5802f6c37c',
name:'aaaa',
positive_point: 150,
nagative_point: 0,
},
{ _id: 58087165f36d2847090a6a26,
user_id: '580da5653bd3cc5802f6c37c',
name:'aaaa',
positive_point: 0,
nagative_point: 50,
},
{ _id: 58087193f36d2847090a6a49,
user_id: '580da5653bd3cc5802f6c37c',
name:'aaaa',
positive_point: 30,
nagative_point: 150,
},
{ _id: 75f36d2847090a6a36580871,
user_id: '3bd3cc5802f6c37c580da565',
name:'bbbb',
positive_point: 30,
nagative_point: 0,
},
{ _id: 08718436d2847090a6a3e58f,
user_id: '3bd3cc5802f6c37c580da565',
name:'bbbb',
positive_point: 50,
nagative_point: 0,
},
{ _id: 6a3087184367090a6a3e58fd284,
user_id: '2f63bc580c37c580da565d3c',
name:'ccc',
positive_point: 50,
nagative_point: 30,
},
{ _id: 8436706a3087190a6a3e52848fd,
user_id: '2f63bc580c37c580da565d3c',
name:'ccc',
positive_point: 0,
nagative_point: 34,
}

]
``````

pls dont give don vote i m beginner thats wy asked.

i stored that data's in one variable and i put for loop

``````console.log(userdetails.length);
for (i = 1; i < userdetails.length; i++) {
console.log(userdetails[i].user_id );
}
``````

but console m getting like this

my expectation result:

``````{
"user_id": "580da5653bd3cc5802f6c37c",
"name": "aaaa",
"totel_positive_point": 60,
"totel_negative_point": 200,
"remaining_point": -140

},
{
"user_id": "3bd3cc5802f6c37c580da565",
"name": "bbbb",
"totel_positive_point": 80,
"totel_negative_point": 0,
"remaining_point": 80

},
{
"user_id": "2f63bc580c37c580da565d3c",
"name": "ccc",
"totel_positive_point": 50,
"totel_negative_point": 64,
"remaining_point": -14

}
``````

Something like this?

``````var data = [
{
_id:            '6d2847090a6a1b',
user_id:        '580da5653bd3cc5802f6c37c',
name:           'aaaa',
positive_point: 150,
negative_point: 0
},
{
_id:            '58087165f36d2847090a6a26',
user_id:        '580da5653bd3cc5802f6c37c',
name:           'aaaa',
positive_point: 0,
negative_point: 50
},
{
_id:            '58087193f36d2847090a6a49',
user_id:        '580da5653bd3cc5802f6c37c',
name:           'aaaa',
positive_point: 30,
negative_point: 150
},
{
_id:            '75f36d2847090a6a36580871',
user_id:        '3bd3cc5802f6c37c580da565',
name:           'bbbb',
positive_point: 30,
negative_point: 0
},
{
_id:            '08718436d2847090a6a3e58f',
user_id:        '3bd3cc5802f6c37c580da565',
name:           'bbbb',
positive_point: 50,
negative_point: 0
},
{
_id:            '6a3087184367090a6a3e58fd284',
user_id:        '2f63bc580c37c580da565d3c',
name:           'ccc',
positive_point: 50,
negative_point: 30
},
{
_id:            '8436706a3087190a6a3e52848fd',
user_id:        '2f63bc580c37c580da565d3c',
name:           'ccc',
positive_point: 0,
negative_point: 34
}
];

// sums
var sum = function (items, prop) {
return items.reduce(function (a, b) {
return a + b[prop];
}, 0);
};

// get all records matching user_id
var filterUser = function (dataset, user_id) {
return dataset.filter(function (el) {
return el.user_id === user_id
});
};

// store unique users by user_id in an array
var uniqueUsers = [];
data.forEach(function (el) {
if (uniqueUsers.indexOf(el.user_id) === -1) {
uniqueUsers.push(el.user_id)
}
});

// loop over unique users and calculate positive, negative and remaining points
var output = [];
for (var i = 0, imax = uniqueUsers.length; i < imax; i++) {

var user = filterUser(data, uniqueUsers[i]);
var positive = sum(user, 'positive_point');
var negative = sum(user, 'negative_point');

output.push({
user_id: uniqueUsers[i],
name: user[0].name,
total_positive_point: positive,
total_negative_point: negative,
remaining_points: positive - negative
});

}

console.log(output);``````