I am new in service now and I don't know yet how to manipulate the data unlike in MySQL and SQL. I just want to know how can I group by the data.
Here is my code to show the data:
var group = new GlideRecord("table");
group.addEncodedQuery('u_active=True');
group.orderBy('u_order_in_services');
group.query();
while (group.next()) {
gs.info(group.group_name);
}
The result is:
Group 1
Group 1
Group 2
Group 2
Group 3
Group 3
Needed result is:
Group 1
Group 2
Group 3
Use GlideAggregate (instead of of GlideRecord) if you need a GROUP BY.
There is an introduction here:
https://developer.servicenow.com/blog.do?p=/post/glideaggregate/
You could use GlideQuery for this, example:
var group = new global.GlideQuery('sys_user_group')
.where('active', true)
.orderBy('name')
.select('name')
.toArray(100);
GQ.jsonDebug(group)
GlideQuery API Documentation
With either GlideQuery or GlideAggregate, to get the result you want you should use the groupBy method. It's hard to tell precisely what you're doing since you didn't give us the actual table name, but, following your example, the code should look like this:
new GlideQuery('table')
.where('u_active', true)
.groupBy('group_name')
.aggregate('count')
.select()
.map(function (record) { return record.group.group_name })
.forEach(GQ.debug);
A comparable GlideAggregate example would look like this:
var ga = GlideAggregate('table');
ga.addQuery('u_active', true);
ga.groupBy('group_name');
ga.addAggregate('COUNT')
ga.query();
while (ga.next()) {
gs.debug(ga.getValue('group_name'));
}
Note, I removed the orderBy clause of both these queries since it makes less sense when we're grouping the results. Also, if this really is a custom table, I would triple-check that the group_name field isn't actually named u_group_name. If so, you'll need to update my examples to work properly.
Related
I'd like to know how to "double sort" items in react native in order to obtain a double criteria sorted list.
The data I receive from BE is a list of match: every match is an object containing 2 players, a time and a competition.
I d'like to sort items by time (so that a today match is showed before a tomorrow match) and by competition (so that a more important competition is showed before a lower one).
If two or more matches are played on the same day and within the same competition, the earlier match is showed before.
A schema better shows what I mean: I'd like to customize the UI oc the day-row, the competition-row and the match-row.
The data I receive from BE are formatted in this way:
How could I obtain what I want? I've tried with sortable list but with no result.
TY in advance
The javascript docs for sort show that you can sort using an arbitrary function. I would write a function which first compares days and then compares competitions:
function compareByDayThenByCompetition(a, b) {
if (a.day < b.day) {
return -1;
}
if (a.day > b.day) {
return 1;
}
return a.competition - b.competition;
}
events = [{day:3, competition:2}, {day:3, competition:3}, {day:3, competition:1}, {day:2, competition:1}]
console.log(events.sort(compareByDayThenByCompetition))
The best way to filter such data is.
Filter the complete list and make an array of object the object should be grouped by same timestamp.
2)Now iterate the filtered object array and filter the object value.
I've looked through a lot of similar answers to this question but have not gotten any solutions to work.
I have two separate functions with one array each, that I populate like this:
folderMap.push([{siteurl: filename,logid: fileurl}]);
mapping.push([{siteurl: x[0],shareddriveid: x[1]}]);
I want to have a third function that I want to match the items based on siteurl and put that object in one array while putting any matches that don't have all three items (a siteurl, logid, and shareddriveid) into another. The source arrays will be different sizes. My current code looks like this:
const combined = folderMap.concat(mapping);
const applicable = combined.filter(i => i[0]['siteurl'] && i[0]);
const byTeamUrl = combined.reduce((acc, item) => {
acc[item['siteurl']] = Object.assign(acc[item['siteurl']] || {}, item);
if (item.length > 2) {
Logger.log(item);
}
return acc;
}, {});
Nothing is output, but I know that there should be at least three items that are in both source arrays. What's going wrong here?
Google Apps Script doesn't currently support ECMAScript 2015 (ES6). Hence your arrow functions will not work.
Per Google's docs: Basic JavaScript Features
See this answer: https://stackoverflow.com/a/51628532/8245557
However, you can develop for Apps Script in TypeScript using clasp: Develop Apps Script using TypeScript
I was pushing an array to my object when I should have just been pushing fields. I also reformatted my code so the same object was passed from function 1 to function 2 where function 2 used findIndex to update the value.
I'm trying to group and add the similar elements in an array that I'm displaying in ng-repeat..
Plunker:Group Array Items
I tried to apply filter as:
<div ng-repeat="shape in shapes|filter:groupFilter">
{{shape.name}}- {{shape.value}}
</div>
$scope.groupFilter=function(item)=>{
return item.name===item.name;
}
where I'm not able to access the whole elements at a time so that I can compare the values and add them up..
The end result that I'm expecting is like this...
Circle- 17
Rectangle- 13
edit
Updated plunkr - http://jsfiddle.net/2s9ogyh8/1/
After reading the question again I realized that you were trying to group not only visually but you also wanted to aggregate values. If you were simply trying to group existing values, then you'd use my original answer.
Since you're aggregating the totals, I'd recommend doing so before the UI renders it, otherwise you encounter the $digest loop issue from #toskv's answer. His aggregating logic is pretty much the same though.
end edit
Here is a related post on this topic - orderBy multiple fields in Angular
Take a look at the built-in orderBy filter - https://docs.angularjs.org/api/ng/filter/orderBy
If you're looking for something more than just ordering by the name/type of shpae then you most likely won't be able to do this via a declarative object matcher but you can pass your groupFilter function back to it.
Updated plunker - http://jsfiddle.net/d13zmk3y/2/
A simple way to do it would be to make filter that uses reduce to group everything.
app.filter('group', function() {
return function(items) {
return items.reduce(function(aux, current) {
if (aux[current.name]) {
aux[current.name].count += parseInt(current.value, 10);
} else {
aux[current.name] = {
name: current.name,
count: parseInt(current.value, 10)
};
}
return aux;
}, {});
};
});
You can see a fully working example here.
I'd like to use the filters to return all values that have an empty string for a particular field.
That is, in the code:
var groups = $filter('unique')(addresses, 'country');
var groupedByCountry = [];
angular.forEach(groups, function (value, key) {
var selectedGroup = value['country'];
var grouped = $filter('filter')(addresses, { country: selectedGroup });
this.push(grouped);
}, groupedByCountry);
As can be seen in http://jsfiddle.net/b7cjM/, it creates groups as expected where a country is specified, but I'd like the last group to contain only the addresses that have no country specified (instead of, as currently, a group of all addresses in existence).
Is this possible using angularjs?
**
As suggested by punund, I wrote a 'groupBy' filter [a naive conversion of the 'unique' filter: http://jsfiddle.net/b7cjM/1 ], which is a much simpler solution to my problem than the one I was attempting. I suspect a custom 'filterByEmpty' of this type is also the solution for the problem as presented.
underscore or lodash may work better for you:
groupedBy = _.groupBy($scope.addresses, 'country');
Empty strings are properly taken into account, and you don't need the second filter. Another option would be to implement a kind of "groupBy" angular filter yourself.
I recently started working with taffydb. Assuming I have this as my data
db= TAFFY([
{OrderNo:'prod1',range: 3,description:'one two'},
{OrderNo:'prod2',range: 2,description:'one two three'},
{OrderNo:'prod3',range: 2,description:'one three two'},
{OrderNo:'prod4',range: 6,description:'one two four three'},
{OrderNo:'prod5',range: 5,description:'three'},...
if I wanted to write a query to find all records with "one two" and "three" I'd do something like
db({description:{likenocase:"one two"}},{description:{likenocase:"three"}}).get()
this would return products 2 and 4. Unfortunately I can't figure out how to do this with a dynamic query that will have an unknown number of variables to search for. I'm doing this to let the user search for their own supplied words.
Anyone got any ideas?
As a precursor, this will not be the best answer to your problem. But it will work. :)
So the user will have the option of searching a database with an "unknown number of variables". Let's add a maximum amount of variables--perhaps 10?
Now we catch all the user's search variables in an array:
// Create a dynamic array
var userSearchVars = [];
// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
userSearchVars.push( $(this).val());
}
// Note: by default an empty input will return the empty string: ""
Using your code snippet, just query the database with the array:
db(
{description:{likenocase:userSearchVars[0]}},
{description:{likenocase:userSearchVars[1]}},
{description:{likenocase:userSearchVars[2]}},
{description:{likenocase:userSearchVars[3]}},
{description:{likenocase:userSearchVars[4]}},
{description:{likenocase:userSearchVars[5]}},
{description:{likenocase:userSearchVars[6]}},
{description:{likenocase:userSearchVars[7]}},
{description:{likenocase:userSearchVars[8]}},
{description:{likenocase:userSearchVars[9]}}
).get()
Adapting #Jacob-IT's answer so that its dynamic. Used Taffy for the first time tonight and just discovered you can pass an array of objects as the query argument.
// Create a dynamic array
var userSearchVars = [];
// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
// This is my edit - push the whole query on to the array.
userSearchVars.push({description:{likenocase: $(this).val() }});
}
// Then pass the whole query array in...
db( userSearchVars ).get()
Tested the above - and it worked for me.
You can do it like this
let items = [];
items.push({description:{likenocase:"one two"}});
items.push({description:{likenocase:"three"}});
db(...items).get()