I have schema:
{
userName: String
postCount: Number,
commentCount: Number,
abuseCount: Number,
}
I need get sum all users counts all fields in result
example
{
"userName": "John"
"postCount": 5,
"commentCount": 1,
"abuseCount": 4,
}, {
"userName": "Bob"
"postCount": 11,
"commentCount": 41,
"abuseCount": 3,
}, {
"userName": "Alex"
"postCount": 2,
"commentCount": 15,
"abuseCount": 6,
}
result must be ~
{
"postCount": 18, (sum all postCount field users)
"commentCount": 57,
"abuseCount": 10,
}
How I can do this? Thx!
Related
I have a json data like this.
[{
"name": "Peter",
"age": 30,
"hair color": "brown"
}, {
"name": "Steve",
"age": 55,
"hair color": "blonde"
}, {
"name": "Steve",
"age": 25,
"hair color": "black"
}]
My code it does is, It will identify the duplicate and remove the second occurrence.
The code is :
var qdata = [{
"name": "Peter",
"age": 30,
"hair color": "brown"
}, {
"name": "Steve",
"age": 55,
"hair color": "blonde"
}, {
"name": "Steve",
"age": 25,
"hair color": "black"
}]
data = qdata.filter((obj, pos, arr) => {
return arr.map(mapObj =>
mapObj.name).indexOf(obj.name) == pos;
});
console.log(data);
Output will be :
[
{ name: 'Peter', age: 30, 'hair color': 'brown' },
{ name: 'Steve', age: 55, 'hair color': 'blonde' }
]
Here it deletes the second occurrence and keeps the first one, But what I would like to get is remove the first occurrence and keep the second one
[
{ name: 'Peter', age: 30, 'hair color': 'brown' },
{ name: 'Steve', age: 25, 'hair color': 'black' }
]
How can I do that ? Please help
You can simply reverse the array and do what you want and reverse the result to match with your expected result.
qdata.reverse();
data = qdata.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj.name).indexOf(obj.name) == pos;
});
console.log(data.reverse());
if you don't want to do that you can use the below code to get desired result.
data = [];
qdata.map((i) => {
index = data.findIndex((u) => u.name === i.name);
if (index >= 0) {
data.splice(index, 1, i);
} else {
data.push(i);
}
});
console.log(data);
In ArangoDB I want to group and sort notification data.
I have the following notification data sets
[
{id: 1, groupId: 1, text: 'Aoo', time: 23},
{id: 2, groupId: 2, text: 'Boo', time: 32},
{id: 3, groupId: 1, text: 'Coo', time: 45},
{id: 4, groupId: 3, text: 'Doo', time: 56},
{id: 5, groupId: 1, text: 'Eoo', time: 22},
{id: 6, groupId: 2, text: 'Foo', time: 23}
]
I want to group the notification by groupId and the recent notification group should appear on top.
Final result should be like this
[
{ groupId: 3, notifications: [{id: 4, groupId: 3, text: 'Doo', time: 56}],
{ groupId: 1, notification: [{id: 3, groupId: 1, text: 'Coo', time: 45}, {id: 1, groupId: 1, text: 'Aoo', time: 23}, {id: 5, groupId: 1, text: 'Eoo', time: 22}]},
{ groupId: 2, notifications: [{id: 2, groupId: 2, text: 'Boo', time: 32}, {id: 6, groupId: 2, text: 'Foo', time: 23}] }
]
Tried following AQL
FOR doc IN notificaion
SORT doc.time DESC
COLLECT groupId = doc.groupId INTO g
RETURN { groupId, notifications: g[*].doc }
Above query sorts the inner group elements but the outer groups are not sorted.
I'm struggling to construct an AQL for it. Any pointer will be helpful.
Thanks
Sort twice: once the set of documents collected - as you already do, then the collection:
FOR doc IN notification
SORT doc.time DESC
COLLECT groupId = doc.groupId INTO g
SORT g[*].doc.time DESC
RETURN { groupId, notifications: g[*].doc }
In my tests this yields the desired sequence:
[
{
"groupId": 3,
"notifications": [
{
"id": 4,
"groupId": 3,
"text": "Doo",
"time": 56
}
]
},
{
"groupId": 1,
"notifications": [
{
"id": 3,
"groupId": 1,
"text": "Coo",
"time": 45
},
{
"id": 1,
"groupId": 1,
"text": "Aoo",
"time": 23
},
{
"id": 5,
"groupId": 1,
"text": "Eoo",
"time": 22
}
]
},
{
"groupId": 2,
"notifications": [
{
"id": 2,
"groupId": 2,
"text": "Boo",
"time": 32
},
{
"id": 6,
"groupId": 2,
"text": "Foo",
"time": 23
}
]
}
]
"orgId": 1,
"orgName":"orgOne"
"empList": [
{
"empId": 1,
"empName":"emp1"
},
{
"empId": 2,
"empName":"emp2"
},
{
"empId": 3,
"empName":"emp3"
}
]
},
{
"orgId": 2,
"orgName":"orgTwo"
"empList": [
{
"empId": 2,
"empName":"emp2"
},{
"empId": 3,
"empName":"emp3"
}
]
}
The following query gives orgId for empId =2
SELECT d.orgId
FROM default AS d
WHERE ANY e IN d.empList SATISFIES e.empId = 2 END;
If you are looking different could you explain more details.
The shape of the documents I'm using look similar to this:
{
"survey_id": 123,
"numerical_answers": [
{
"component_id": 345,
"number": 5,
},
{
"component_id": 346,
"number": 5,
}
]
}
The output of ArangoDB should look like this:
[
{
"component_id": 345,
"distribution": [
{
"component_id": 345,
"score": null,
"total": 42
},
{
"component_id": 345,
"score": 1,
"total": 76
},
{
"component_id": 345,
"score": 2,
"total": 37
},
{
"component_id": 345,
"score": 3,
"total": 40
},
{
"component_id": 345,
"score": 4,
"total": 93
},
{
"component_id": 345,
"score": 5,
"total": 212
}
],
"total": 500,
"avg": 3.404
}
]
My AQL looks like this:
FOR doc IN ##collection
FILTER doc.`survey_id` == #survey_id
LET componentScoreGroup = {
component_id: #component_id,
score: FIRST(doc.numerical_answers[* filter CURRENT.component_id == #component_id return CURRENT.number])
}
COLLECT component_id = componentScoreGroup.component_id, score = componentScoreGroup.score WITH COUNT INTO total
LET distribution = {
component_id: component_id,
score: score,
total: total
}
COLLECT component_id2 = distribution.component_id into groups keep distribution
LET finalTotal = sum(groups[*].distribution.total),
summedScore = sum(groups[* return CURRENT.distribution.score * CURRENT.distribution.total])
RETURN {
component_id: component_id2,
distribution: groups[*].distribution,
total: finalTotal,
avg: summedScore / finalTotal
}
I would like to use the more performant AGGREGATE syntax in the COLLECT but I'm not sure I can because I also want to pull in the distribution content in the final return.
Any help would be greatly appreciated!
is this closer to what you are looking for?
FOR doc IN ##collection
FILTER doc.survey_id == #survey_id
LET componentScoreGroup = {
component_id: #component_id,
score: FIRST(doc.numerical_answers[* filter CURRENT.component_id == #component_id return CURRENT.number])
}
COLLECT id = componentScoreGroup.component_id
AGGREGATE avg = AVG(componentScoreGroup.score),
total = SUM(componentScoreGroup.score)
INTO group
return { "id" : id , "avg" : avg , "total" : total, "number" : LENGTH(group), "group" : group}
Hey i'm currently trying to create a card game in c++ but i've run into some issues. I've created my deck using a struct called CardStruct. My question first has to do with the shuffeling of a struct. I've tried to shuffle it but I keep getting repeating cards. (You can see my attempt at the end of the code).
struct CardStruct
{
int value;
char suit;
};
int main()
{
srand(time(0));
CardStruct Deck[52] = { { 2, char(6) }, { 3, char(6) }, { 4, char(6) }, { 5, char(6) }, { 6, char(6) }, { 7, char(6) }, { 8, char(6) }, { 9, char(6) }, { 10, char(6) }, { 11, char(6) }, { 12, char(6) }, { 13, char(6) },{ 2, char(5) }, { 3, char(5) }, { 4, char(5) }, { 5, char(5) }, { 6, char(5) }, { 7, char(5) }, { 8, char(5) }, { 9, char(5) }, { 10, char(5) }, { 11, char(5) }, { 12, char(5) }, { 13, char(5) }, { 2, char(4) }, { 3, char(4) }, { 4, char(4) }, { 5, char(4) }, { 6, char(4) }, { 7, char(4) }, { 8, char(4) }, { 9, char(4) }, { 10, char(4) }, { 11, char(4) }, { 12, char(4) }, { 13, char(4) }, { 2, char(3) }, { 3, char(3) }, { 4, char(3) }, { 5, char(3) }, { 6, char(3) }, { 7, char(3) }, { 8, char(3) }, { 9, char(3) }, { 10, char(3) }, { 11, char(3) }, { 12, char(3) }, { 13, char(3) } };
// printCards(Deck);
for (int i = 0; i < 52; i++)
{
int index = rand() % 52;
cout << Deck[index].value << Deck[index].suit << endl;
}
Instead of
{ 2, char(6) }
use
{ 2, 'K' }
K being any char you want.