Groupby array by multiple values - html

I have an array I want to use groupby angular's filter and to group it by an array of criterias. Is there any way to do this? I guess I might use revision but I don't know how.
For example I have array of football teams. Each team have color, number of players, name, city, country.
I have array of data filters -country, city, players and color and I want to use groupby in the order of this array

not sure about angular. but just include underscore (it's more known for array/object manipulation) into your project then:
say you had an array of objects like an array of these:
car = {
make: "nissan",
model: "sunny",
color: "red"
};
then you'd just go:
var redCars = _.groupBy(cars, 'color');

Related

How to return a mysql JSON array of GROUP_CONCAT output?

I have a table mapping departments and teams in MySQL.
I want to retrieve array of teams for each department.
For example, if I have two departments depA (teams teamAA, teamAB), depB (teams teamBA, teamBB, teamBC), I want to obtain following JSON
[
{
code: "depA",
teams: ["teamAA", "teamAB"]
},
{
code: "depB",
teams: ["teamBA", "teamBB", "teamBC"]
}
]
I am able to use GROUP_CONCAT to obtain a concatenated string but, I want a JSON array so that the sequelize.js library I am using can implicitly parse the entire data structure to js object.
It can be done like so,
SELECT `departmentCode` AS `code`, JSON_ARRAY(GROUP_CONCAT(DISTINCT `teamCode`)) AS `teams`

How to check if JSON response fields are alphabetically sorted?

How to validate the JSON Response fields? After validating the fields I need to check whether the fields are alphabetically sorted or not. How would I do it ?
The JSON object is unordered set of name/value pairs. There is no guarangee of ordering at all. You need to take json object keys list and sort them. After sorting access to object fields by sorted keys list.
If you mean how to check list (array) of values. You need to implement simple loop through array and check that each element must be less than next element in sorting comparision criteria.
For js language checking array for alpha ordering may be done like this:
var array = ["Apple", "Application", "AppName", "Happy"];
var isSortedAlpha = array.reduce(function(res, current, index, arr) {
return res && arr[index&&index-1] <= current
}, true);

Laravel - Group By & Key By together

Assuming I have the following MySQL tables to represent pricebooks, items and the relationship between them:
item - item_id|name|...etc
pricebook - pricebook_id|name|...etc
and the following pivot table
pricebook_item - pricebook_id|item_id|price|...etc
I have the correlating Eloquent models: Pricebook, Item and a repository named PricebookData to retrieve the necessary information.
Within the PricebookData repository, I need to get the pricebook data grouped by pricebook id and then keyed by item_id for easy access on client side.
If I do:
Pricebook::all()->groupBy('pricebook_id');
I get the information grouped by the pricebook_id but inside each pricebook the keys are simple numeric index (it arrives as js array) and not the actual product_id. So when returning to client side Javascript, the result arrives as the following:
pricebookData: {1: [{}, {}, {}...], 2: [{}, {}, {}...]}
The problem with the prices arriving as array, is that I can not access it easily without iterating the array. Ideally I would be able to receive it as:
pricebookData: {1: {1001:{}, 1002: {}, 1003: {}}, 2: {1001:{}, 1002: {}, 1003: {}}}
//where 1001, 1002, 1003 are actual item ids
//with this result format, I could simply do var price = pricebookData[1][1001]
I've also tried the following but without success:
Pricebook::all()->keyBy('item_id')->groupBy('pricebook_id');
The equivalent of what I am trying to avoid is:
$prices = Pricebook::all();
$priceData = [];
foreach ($prices as $price)
{
if (!isset($priceData[$price->pricebook_id]))
{
$priceData[$price->pricebook_id] = [];
}
$priceData[$price->pricebook_id][$price->item_id] = $price;
}
return $priceData;
I am trying to find a pure elegant Eloquent/Query Builder solution.
I think what you want is
Pricebook::all()
->groupBy('pricebook_id')
->map(function ($pb) { return $pb->keyBy('item_id'); });
You first group by Pricebook, then each Pricebook subset is keyed by item_id. You were on the right track with
Pricebook::all()->keyBy('item_id')->groupBy('pricebook_id');
unfortunately, as it is implemented, the groupBy resets previous keys.
Update:
Pricebook::all()->keyBy('item_id')->groupBy('pricebook_id', true);
(groupBy second parameter $preserveKeys)

Sorting users in Rails based on values in array attribute of user model

I have a User model which has an array inside of it. This array is used to store points the user has scored in various activities. It basically looks like this:
<ActiveRecord::Relation [#<User id: 1, fullname: "Kaja Sunniva Edvardsen", points: [0, 4170, 3860, 2504, 2971, 3859, 4346]>, #<User id: 2, fullname: "Alexander Lie Sr.", points: [0, 3273, 3681, 2297, 2748, 4202, 3477]>]>
I want to sort all Users by the different values in the points array to be able to create ranking list for each of the different activities, points[0], points[1], etc...
Sorting by points[1] should return Kaja first, 4170>3273, sorting by points[6] should put Alexander first, 4202>3859
How do I do this?
As far as I know, MySQL does not have an integrated array type.
Assuming you have a model like this:
class User < ActiveRecord::Base
# ...
serialize :points, Array
# ...
end
You cannot sort with order queries, but you can try another solution (less efficient), handling the resources as an array:
User.all.sort { |user1, user2| user2.points[1] <=> user1.points[1] }
Which will return an array instead of an ActiveRecord query. Also, bear in mind that this code will not handle nil values (i.e. What if an user only have 2 elements in points?).

Displaying an array comma-separated in jade?

I want to display an array in Jade separated by commas instead of line which I currently have how can I do this? This is segment of code I need help with players being the array passed from javascript
p Currently playing:
ul
each theExit in players
p #{theExit}
Assuming that players is an array of String's you can use following statement:
p Currently playing: #{players.join(', ')}
That will give you something like (assuming you had 3 entries in the array: player1, player2 and player3):
<p>Currently playing: player1, player2, player3</p>
I hope that will help.
Supposed you were working with not plain String arrays and your data is something like below:
{
[_id: 1, name: 'player1'],
[_id: 2, name: 'player2'],
[_id: 3, name: 'player3'],
}
Then you could probably just use:
each player, index in players
if index === players.length -1
| #{players.name}
else
| #{players.name},
NOTE: Not really the cleanest solution out there but does the job. Use at your own disposal. :)
If you have an array of objects and you want a comma-separated list of a property of each object, I went with:
- var playerList = players.map(player => player.name).join(', ')
p Currently playing: #{playerList}