I'm a newbie but i thing this is for Senior Lodash Programmer
I'm new to Lodash and trying to solve this problem but could find a good way to do it.
I have an array of orders and an array of lines with one ticket into each order.
The data is structured as below:
[{
"id": 201,
"order": "Buyer 1",
"lines": ▿[
▿{
"id": 1,
"ticket": ▿{
"id": 151,
"event": ▿{
"id": 31,
"name": "Event 1"
},
"name": "Event 1 Ticket 1",
"price": 39,
"minimum": 1,
"maximum": 5
},
"quantity": 1
},
▿{
"id": 2,
"ticket": {
"id": 152,
"event": {
"id": 31,
"name": "Event 1"
},
"name": "Event 1 Ticket 2",
"price": 60,
"minimum": 2,
"maximum": 4
},
"quantity": 5
},
▿{
"id": 3,
"ticket": {
"id": 153,
"event": {
"id": 33,
"name": "Event 1"
},
"name": "Event 3 Ticket 2",
"price": 60,
"minimum": 2,
"maximum": 4
},
"quantity": 5
}
],
"created_at": "2016-12-22T17:58:27+0000"
},
...]
I need to group all orders by event name and need the next structure:
[{
"event_id": 31,
"event_name":"Event 1",
"total_tickets": 6,
"total_sold": 339
"orders": [
{
"id": 201,
"order": "Buyer 1"
},
....
]},
{
"event_id": 33,
"event_name":"Event 3",
"total_tickets": 5,
"total_sold": 300
"orders":[
{
"id": 201,
"order": "Buyer 1"
}
]},
....
]
"total_tickets" -> sum of order' tickets (quantity) of event
"total_sold" -> sum of order' (price* quantity) of event
Any help will by very usefull
Take a look at _.reduce (or even vainilla JS Array.reduce)
As a friendly advice, try to imagine and sketch - in your mind, or a paper - what exactly needs be done with that data and then start looking for the tools to do it, not the other way around.
Related
I am calling an API from a server, and I am getting this as a response. A part of the data I am getting -
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Finished",
"short": "FT",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Not Started",
"short": "NS",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
There are a bunch of such objects in an array each having a different fixture. As you can see that status?.short === "NS" in one while the other is status?.short === "FT". Now, I want to filter the data using these properties.
<div className="fixturesTab-container">
<div className="fixturesTab-container__header">
<h4>Fixtures</h4>
</div>
<div className="fixturesTab-container__body">
{fixtures.map((fixture) => (
<div
key={fixture?.id}
className="fixturesTab-container__body__box"
>
......
</div>
))}
</div>
</div>
I want the data to be displayed in such a manner that when the user browse to that page and the API is being fetched, eventhough the entire data (fixtures) is being displayed in the page. I want the fixtures that have not yet started, i.e has the property of status?.short === "NS", to be displayed in the window that is visible to the user. And the rest data can be acquired by either scrolling up, which will have the fixtures that have been completed, or by scrolling down which will have the fixtures that have not yet been started.
If, I am unable to understand what I want properly, then you can check out this page . This is exactly what I am planning to achieve.
I have a JSON with documents and each document has a few _childDocuments_ I want to search in the parent and get back the parents and facets on each parent separate
Here is a sample JSON
[{
"id": 1,
"productName": "Ford Explorer",
"_childDocuments_": [{
"id": 6,
"color": "blue",
"price": 1000
},
{
"id": 7,
"color": "red",
"price": 2000
}
]
},
{
"id": 1,
"productName": "Ford F150",
"_childDocuments_": [{
"id": 10,
"color": "blue",
"price": 5000
},
{
"id": 11,
"color": "red",
"price": 6000
}
]
},
{
"id": 2,
"productName": "Toyota Highlander",
"_childDocuments_": [{
"id": 8,
"color": "green",
"price": 1200
},
{
"id": 9,
"color": "red",
"price": 2000
}
]
}
]
I want when I search for Ford I should get back all Fords and in each root document get back facets of the children.
Something like this:
[{
"id": 1,
"productName": "Ford Explorer",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 2000,
"count": 1
},
{
"val": 3000,
"count": 1
}
]
}
}
},
{
"id": 1,
"productName": "Ford F150",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 5000,
"count": 1
},
{
"val": 6000,
"count": 1
}
]
}
}
}
]
This is what I got so far
q={!parent+which=type:parent}&json.facet={ "prices": {
"type": "range",
"field": "price",
"start": 1000,
"end": 1000,
"gap": 1000
}
,
}
But this is only returning facets on all child documents.
Thanks in advance
I wrote this code to get the product information with it's images and category:
->where('category_id', 5)
->with('category', 'Files')->get();
my result is:
{
"id": 2,
"name": "test",
"price": 13000,
"description": "some text ...",
"shop_id": 1,
"rate": 0,
"category_id": 5,
"discount_percent": 20,
"category": {
"id": 5,
"name": "cat1",
"shop_id": 1
},
"files": [
{
"id": 99,
"disk_name": "5ef1af07d6d98778754621.jpg",
"file_name": "13330983_xl.jpg",
"file_size": 69813,
"content_type": "image/jpeg",
"title": null,
"description": null,
"field": "product_gallery",
"sort_order": 99,
"created_at": "2020-06-23 07:28:07",
"updated_at": "2020-06-23 07:28:10",
"path":...... storage/app/uploads/public/5ef/1af/07d/5ef1af07d6d98778754621.jpg",
"extension": "jpg"
}
]
}
now i want to access the path field, how can i do it?
i use this way for access but i don't get result:
products[0].files[0].path
You should use toArray() function to convert data likes this
->where('category_id', 5)
->with('category', 'Files')->get()->toArray();
And then access
products[0]['files'][0]['path']
I have a JSON array as follows
{
"id": "00000005",
"Name": "Test5",
"hours": 7.5,
"day": 1
},
{
"id": "00000005",
"Name": "Test5",
"hours": 2,
"day": 2
},
{
"id": "00000005",
"Name": "Test5",
"hours": 3,
"day": 3
},
{
"id": "00000005",
"Name": "Test5",
"hours": 3,
"day": 4
},
{
"id": "00000004",
"Name": "Test4",
"hours": 1,
"day": 1
},
{
"id": "00000004",
"Name": "Test4",
"hours": 4,
"day": 2
},
{
"id": "00000004",
"Name": "Test4",
"hours": 4,
"day": 3
},
{
"id": "00000003",
"Name": "Test3",
"hours": 7.5,
"day": 1
},
{
"id": "00000003",
"Name": "Test3",
"hours": 6,
"day": 2
},
{
"id": "00000003",
"Name": "Test3",
"hours": 4,
"day": 3
},
{
"id": "00000003",
"Name": "Test3",
"hours": 5,
"day": 4
}
By using the above json array I want to draw a bar chart grouped by id and day. That is I need to the graph to be drawn for id 00000005,00000004,00000003 in day 1 and id 00000005,00000004,00000003 in day 2 and id 00000005,00000004,00000003 in day 3 and id 00000005,00000004,00000003 in day 4.
I know the basic code for drawing a bar chart using angular-chart.js and chart.js. But I can't understand how should I pass my array values to $scope.labels and $scope.data.
Basic code for bar chart
angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
})
UPDATE
I managed to group the array according to the day and following is the arranged array
"data": {
"1": [
{
"id": "00000005",
"Name": "Test5",
"hours": 7.5,
"day": 1
},
{
"id": "00000004",
"Name": "Test4",
"hours": 1,
"day": 1
},
{
"id": "00000003",
"Name": "Test3",
"hours": 7.5,
"day": 1
}
],
"2": [
{
"id": "00000005",
"Name": "Test5",
"hours": 2,
"day": 2
},
{
"id": "00000004",
"Name": "Test4",
"hours": 4,
"day": 2
},
{
"id": "00000003",
"Name": "Test3",
"hours": 6,
"day": 2
}
],
"3": [
{
"id": "00000005",
"Name": "Test5",
"hours": 3,
"day": 3
},
{
"id": "00000004",
"Name": "Test4",
"hours": 4,
"day": 3
},
{
"id": "00000003",
"Name": "Test3",
"hours": 4,
"day": 3
},
],
"4": [
{
"id": "00000005",
"Name": "Test5",
"hours": 3,
"day": 4
},
{
"id": "00000003",
"Name": "Test3",
"hours": 5,
"day": 4
}
]
}
}
Now how can I assign these values to $scope.data and $scope.labels ?
If you take a run at the basic code, applied to your data, do you want something like this (if I were reporting on this data):
angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
$scope.labels = ['00000005', '00000004', '00000003'];
$scope.series = ['Day 1', 'Day 2', 'Day 3', 'Day 4'];
$scope.data = [
[7.5, 1, 7.5],
[2, 4, 6],
[3, 4, 4],
[3, ?, 5],
];
})
Note entirely sure how you want to handle missing data, you'll have to play around with a combination of 0 or undefined within the chart. If you have issues splitting the data in this way, or this isn't what you're looking for, will need more details.
https://plnkr.co/edit/ouHu5R0UbbkxyDfUerru?p=preview
I have 2 Collections/ Result sets the first one is products and second one is sizes
"products": [
{
"category_id": 5,
"id": 5,
"code": "A",
"name": "Pizzabrot",
"description": "",
"product_prices": [
{
"product_id": 5,
"price": 2.5,
"size_id": 15
},
{
"product_id": 5,
"price": 3.5,
"size_id": 16
}
]
},
{
"category_id": 5,
"id": 6,
"code": "B",
"name": "Pizzabrot mit Knoblauch",
"description": "",
"product_prices": [
{
"product_id": 6,
"price": 3,
"size_id": 15
},
{
"product_id": 6,
"price": 4,
"size_id": 16
}
]
}]
AND
"sizes": [
{
"id": 15,
"name": "Klein",
"category_id": 5
},
{
"id": 16,
"name": "Gro\u00df",
"category_id": 5
}
]
I want to replace every product_prices.size_id with it's name from sizes Collection
From what I can see it would probably be better to associate ProductPrices and Sizes and fetch them with your Products.
If this does not fit your needs for some reason, you could find the sizes by using a find('list') (see Docs) like this:
$query = $this->Sizes->find('list', [
'keyField' => 'id',
'valueField' => 'name'
]);
$sizes = $query->toArray();
Then loop through the products and its product_prices and do something like.
$product_price->size_id = $sizes[$product_price->size_id];
Please prefer the first solution. ;-)