vue.js - how to create a nested arrays - html

i want to calculate the total costs of products selected by a user under each company.There is my code.
let companiesProductPrices = [];
let prices = [];
this.selectedCompaniesDetails.forEach(company => {
this.chosenItems.forEach(item=> {
item.product_attributes.forEach(product => {
if(company.id == product.company_id)
{
prices.push(product.price);
companiesProductPrices[company.id] = prices;
}
})
});
})
Background information. I have to objects. The products object and the companies object.
Products object referred to as this.chosenItems in my code and is like this;
[
{
"id": 1,
"name": "Zimgold",
"slug": null,
"product_category_id": 1,
"industry_id": 1,
"category": {
"id": 1,
"name": "Cooking Oil",
"slug": null,
"industry_id": 1
},
"product_attributes": [
{
"id": 1,
"description": "2 litres",
"discount": null,
"price": "120",
"tax": null,
"company_id": 1,
"product_id": 1
},
{
"id": 5,
"description": "2 litres",
"discount": null,
"price": "130",
"tax": null,
"company_id": 2,
"product_id": 1
}
]
},
{
"id": 4,
"name": "Silo",
"slug": null,
"product_category_id": 3,
"industry_id": 1,
"category": {
"id": 3,
"name": "Mealie Meal",
"slug": null,
"industry_id": 1
},
"product_attributes": [
{
"id": 4,
"description": "10 kgs",
"discount": null,
"price": "130",
"tax": null,
"company_id": 1,
"product_id": 4
}
]
}
]
and my company object referred to as this.selectedCompaniesDetails in my code and is like this.
[
{
"id": 1,
"name": "Spar",
"slug": null,
"industry_id": 1
},
{
"id": 2,
"name": "Ok",
"slug": null,
"industry_id": 1
},
{
"id": 3,
"name": "TM",
"slug": null,
"industry_id": 1
},
{
"id": 4,
"name": "choppies",
"slug": null,
"industry_id": 1
},
{
"id": 5,
"name": "Bon Marche",
"slug": null,
"industry_id": 1
}
]
I'm wishing to get the totals of all products that are belong to each company. Let me show you my UI.
So what i want is under spar the total should be 250 and under 130 and so forth for all other companies. Thank you in advance

This is one of many solutions. It's improvable, it's just to show you the way.
#1 Create an object where key is company id and value is the array of prices for that company
const priceByCompany = products.reduce((tot, prod) => {
prod.product_attributes.forEach((p) => {
if (!tot[p.company_id]) {
tot[p.company_id] = []
}
const numberPrice = parseInt(p.price, 10)
tot[p.company_id].push(numberPrice)
})
return tot
}, {})
// { 1: [120, 130], 2: [130] }
#2 Reduce all value array to sum them up.
Object.keys(priceByCompany).forEach((company) => {
const total = priceByCompany[company].reduce((tot, price) => tot + price, 0)
priceByCompany[company] = total
})
// { 1: 250, 2: 130 }
See the code in action: Jsfiddle

Related

Getting JSON data in Groovy

I need to get some datas from JSON, I could manage to transform it into String. For example, I need to get the amount value if team role id is 4.( The last scope in the JSON.) When I run the code below, the "result" output is
{id=1, effectiveDate=2003-01-01, currencyCode=USD, rates=[{id=1, rateTable={id=1, effectiveDate=2003-01-01, currencyCode=USD, name=Tempo Default Price Table, defaultTable=false}, amount=0.0, link={type=DEFAULT_RATE}}], name=Tempo Default Price Table, defaultTable=true}
How can I get the whole data?
Thanks.
http.request(Method.GET) {
response.success = { resp, json ->
arrayDen = JsonOutput.toJson(json).substring(1, JsonOutput.toJson(json).length()-1)
}
}
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(arrayDen)
log.warn(result)
[
{
"id": 1,
"rateTable": {
"id": 1,
"effectiveDate": "2003-01-01",
"currencyCode": "USD",
"name": "Tempo Default Price Table",
"defaultTable": false
},
"amount": 0.0,
"link": {
"type": "DEFAULT_RATE"
}
},
{
"id": 2,
"rateTable": {
"id": 3,
"effectiveDate": "2022-03-21",
"currencyCode": "USD",
"name": "Rate",
"defaultTable": false
},
"amount": 0.0,
"link": {
"type": "DEFAULT_RATE"
}
},
{
"id": 3,
"rateTable": {
"id": 3,
"effectiveDate": "2022-03-21",
"currencyCode": "USD",
"name": "Rate",
"defaultTable": false
},
"amount": 200.0,
"link": {
"type": "TEAM_ROLE",
"id": 8
}
},
{
"id": 4,
"rateTable": {
"id": 3,
"effectiveDate": "2022-03-21",
"currencyCode": "USD",
"name": "Rate",
"defaultTable": false
},
"amount": 500.0,
"link": {
"type": "TEAM_ROLE",
"id": 5
}
},
{
"id": 5,
"rateTable": {
"id": 3,
"effectiveDate": "2022-03-21",
"currencyCode": "USD",
"name": "Rate",
"defaultTable": false
},
"amount": 1000.0,
"link": {
"type": "TEAM_ROLE",
"id": 4
}
}
]
`
Hmmm. The following worked fine for me:
def arrayDen = '<YOUR_URL_GOES_HERE>'.toURL().text
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(arrayDen)
def desiredData = result.find { it.id == 4 }
println desiredData.amount
You might give it a try.

How to match the no of responses returned with a value displayed in the response body

I have the below response in my postman Body .
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"id": "c2004ef952894279920417ba8283d26a",
"name": "attrib_inv_bu",
"modified_date": "2020-07-06T02:15:06.839577",
"display_group": {
"id": "attr_dis_267a405426184764816e504b4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8283d26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2707/"
},
{
"id": "c2004ef95289422324232a8283d26a",
"name": "attrib_idsdw_sfs",
"modified_date": "2020-07-06T02:11:06.839577",
"display_group": {
"id": "attr_dis_267a4054261dfsf64816e504b4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8ssd3d26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2709/"
},
{
"id": "c2004ef9we22e2e8283d26a",
"name": "attrib_iasa_ac",
"modified_date": "2020-07-06T02:17:06.839577",
"display_group": {
"id": "attr_dis_267a405426184764816e5dsds4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8283sds26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2708/"
}
]
}
I want to assert whether I have 3 different ids match with the count 3 that is displayed in the body.
res = pm.response.json()
res.count === res.results.length
you can use in expect as :
pm.expect(res.count).to.be.equal(res.results.length)
If you want to verify they are indeed unique:
let resultidlits = []
result.forEach((a) => {
resultidlits.includes(a.id)? null : resultidlits.push(a.id)
})
pm.expect(res.count).to.be.equal(resultidlits.length)

Order Only Nested Model Data Sequelize

I am trying to order only the nested include Scheme Model based on an integer column but it seems like the code is not working. Also, the data is being fetched from MYSQL bottom to up and shown as output.
sample code:
[err, products] = await to(
Product_Inventory_Mapping.findAll({
include: [
{
model: Product,
as: "product_details",
where: filterClause,
include: [
{
model: Unit,
as: "unit_details"
},
{
model: Brand,
as: "brand_details"
},
{
model: Category,
as: "category_details"
},
{
model: Sub_Brand,
as: "sub_brand_details"
},
{
model: Sub_Category,
as: "sub_category_details"
},
{
model: Brand_Company,
as: "brand_company_details"
},
{
model: Scheme,
as: "schemes",
attributes: [`moq`, 'discount', `description`],
order: [[{ model: Scheme, as: "schemes" }, "moq", "ASC"]],
where: {
status: 1
},
required: false
}
]
}
],
order: [
[{ model: Product, as: "product_details" }, "mrp", "ASC"]
],
where: {
user_id: aligned_distributors,
brand_company_id: filtered_brand_company,
count: {
[Op.gt]: 0
},
sp: {
[Op.gt]: 0
}
},
attributes: [
`product_id`,
"user_id",
"count",
"sp",
"occ",
"brand_company_id"
],
limit: limit,
offset: offset,
subQuery: false
}));
Here I want to order only the data inside the Scheme Model.
Sample response:
"products": [
{
"product_id": 115,
"user_id": 1003,
"count": 50,
"sp": 9.09,
"occ": 9.09,
"brand_company_id": 11,
"product_details": {
"id": 115,
"category_id": 2,
"sub_category_id": 12,
"brand_company_id": 11,
"brand_id": 24,
"sub_brand_id": 44,
"sku_code": null,
"min_order_qty": "27",
"description": "Nandini Butter Milk Tetra Pack, 200 Ml",
"unit_id": 4,
"weight": "200ml",
"mrp": "10.00",
"barcode": "",
"image_url": "https://xxxxxx.s3.ap-south-1.amazonaws.com/products/1565919229Unknown-min-5.jpg",
"created_at": "2019-08-12T13:44:01.000Z",
"updated_at": "2019-08-15T20:03:49.000Z",
"unit_details": {
"id": 4,
"name": "Pieces",
"created_at": null,
"updated_at": null
},
"brand_details": {
"id": 24,
"name": "Nandini Butter Milk",
"brand_company_id": 11,
"created_at": null,
"updated_at": null
},
"category_details": {
"id": 2,
"name": "Packaged Food",
"image_url": "https://zzzz.s3.ap-south-1.amazonaws.com/images/category/food-min.png",
"created_at": "2019-08-11T11:36:52.000Z",
"updated_at": "2019-08-11T11:36:52.000Z"
},
"sub_brand_details": {
"id": 44,
"name": "Nandini Butter Milk",
"brand_id": 24,
"created_at": null,
"updated_at": null
},
"sub_category_details": {
"id": 12,
"name": "Dairy products (Butter, Cheese etc)",
"category_id": 2,
"created_at": null,
"updated_at": null
},
"brand_company_details": {
"id": 11,
"name": "Karnataka Co-operative Milk Producers",
"image_url": "https://xxxxx.s3.ap-south-1.amazonaws.com/images/company/kmf_logo.jpg",
"created_at": "2019-08-12T13:10:18.000Z",
"updated_at": "2019-08-12T13:10:18.000Z"
},
"schemes": [
{
"moq": 30,
"discount": 1.5,
"description": "8 % offer"
},
{
"moq": 20,
"discount": 1,
"description": "20 % offer"
},
{
"moq": 40,
"discount": 2,
"description": "40 % offer"
}
]
}
}]
In the database, the Scheme data is saved as:
Scheme belongs to the Product via product_id. I want the Scheme array to be sorted by moq ASC so that the value comes in order of moq: 20 > 30 > 40.
AFAIK, an order clause in the included model doesn't work. But, you can make a reference to the included model in the main model. I think this should work if you combine your two order clauses into one:
Product_Inventory_Mapping.findAll({
include: [
..... lots of includes here ....
],
order: [
[{ model: Product, as: "product_details" }, "mrp", "ASC"],
[{ model: Scheme, as: "schemes" }, "moq", "ASC"]]
],
This will order by moq within mrp... you could remove the first field if you really want to sort ONLY by moq....

How to set alias instead of id , i need category_id without using forloop

$categoryData = $this->compCatObj->find()->select(['CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}
i need category_id instead of id. Is there any way to do this without using forloop.
yes you can do this by a small change in your code,
$categoryData = $this->compCatObj->find()->select(['category_id' => 'CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"category_id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"category_id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}

How to filter Inner Objects in Elasticsearch?

I have a contacts field in my documents in Elasticsearch. each element inside the contacts field is an Object itself. I want to use term or terms filter on contacts field so that it matches documents where contacts.province_id is X. I have tried contacts.province_id as search field but it doesn't work. How should I filter these kinds of fields?
"contacts":
[
{
"id": 1,
"address": "address1",
"tel": "40 07 13 22",
"doctor_id": 1,
"type_id": 1,
"lng": "51.374720",
"lat": "35.781986",
"city_id": 186,
"province_id": 8,
"hour_about": null,
"place_name": null
},
{
"id": 2,
"address": "address2",
"tel": null,
"doctor_id": 1,
"type_id": 2,
"lng": "51.520313",
"lat": "35.726983",
"city_id": 186,
"province_id": 8,
"hour_about": null,
"place_name": null
},
{
"id": 3,
"address": "address3",
"tel": null,
"doctor_id": 1,
"type_id": 2,
"lng": "51.456368",
"lat": "35.797505",
"city_id": 186,
"province_id": 8,
"hour_about": null,
"place_name": null
}
]
EDIT :
I've tried this Query :
GET /index_name/type_name/_search
{
"query": {
"filtered": {
"filter": {
"term": {
"contacts.province_id": 8
}
}
}
}
}
but it returns 3 results, I expect 5 results. whats the problem?
Thanks for your help.
What is the mapping you have used? If you are using nested type then try using the nested filter.
{
"filter": {
"nested": {
"path": "contacts",
"filter": {
"term": {
"contacts.province_id": 3
}
}
}
}
}