This question already has answers here:
How to find object in array by property in javascript?
(3 answers)
Closed 2 years ago.
I have the Basic Recipe array with the details objects:
I want to be able to update the value of specific item inside the details i.e item with ID 5f3aadd5d756e9341ef74e2b
{
"brandName": "Piatto",
"_id": "5f3aadd5d756e9341ef74e29",
"name": "Test",
"rate": 89,
"baseQuantity": 1000,
"baseUnit": "gm",
"details": [
{
"_id": "5f3aadd5d756e9341ef74e2b",
"rawMaterial": "Egg white 1",
"brandName": "Bisleri",
"supplier": "Dasnya",
"type": "Solid",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 12,
"quantityInRecipe": 0,
"costOfRawMaterial": 0,
"recipeUnit": "ml",
"displayRateUnit": "1/2 lit."
},
{
"_id": "5f3aadd5d756e9341ef74e2a",
"rawMaterial": "Egg white 2",
"brandName": "Bisleri",
"supplier": "Dasnya",
"type": "Solid",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 14,
"quantityInRecipe": 0,
"costOfRawMaterial": 0,
"recipeUnit": "gm",
"displayRateUnit": "1/2 lit."
}
],
"__v": 0
}
Please help!
Try this.
var data = {
"brandName": "Piatto",
"_id": "5f3aadd5d756e9341ef74e29",
"name": "Test",
"rate": 89,
"baseQuantity": 1000,
"baseUnit": "gm",
"details": [{
"_id": "5f3aadd5d756e9341ef74e2b",
"rawMaterial": "Egg white 1",
"brandName": "Bisleri",
"supplier": "Dasnya",
"type": "Solid",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 12,
"quantityInRecipe": 0,
"costOfRawMaterial": 0,
"recipeUnit": "ml",
"displayRateUnit": "1/2 lit."
},
{
"_id": "5f3aadd5d756e9341ef74e2a",
"rawMaterial": "Egg white 2",
"brandName": "Bisleri",
"supplier": "Dasnya",
"type": "Solid",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 14,
"quantityInRecipe": 0,
"costOfRawMaterial": 0,
"recipeUnit": "gm",
"displayRateUnit": "1/2 lit."
}
],
"__v": 0
};
var idToBeUpdate = '5f3aadd5d756e9341ef74e2b'
data.details.forEach(x => {
if (x._id === idToBeUpdate) {
x.rawMaterial = "Egg white 3 <updated>"
}
});
console.log(data);
Ciao, you could use a filter function to get element you want to update based on _id, and then modify it like:
let input = {
"brandName": "Piatto",
"_id": "5f3aadd5d756e9341ef74e29",
"name": "Test",
"rate": 89,
"baseQuantity": 1000,
"baseUnit": "gm",
"details": [
{
"_id": "5f3aadd5d756e9341ef74e2b",
"rawMaterial": "Egg white 1",
"brandName": "Bisleri",
"supplier": "Dasnya",
"type": "Solid",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 12,
"quantityInRecipe": 0,
"costOfRawMaterial": 0,
"recipeUnit": "ml",
"displayRateUnit": "1/2 lit."
},
{
"_id": "5f3aadd5d756e9341ef74e2a",
"rawMaterial": "Egg white 2",
"brandName": "Bisleri",
"supplier": "Dasnya",
"type": "Solid",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 14,
"quantityInRecipe": 0,
"costOfRawMaterial": 0,
"recipeUnit": "gm",
"displayRateUnit": "1/2 lit."
}
],
"__v": 0
}
let elementToUpdate = input.details.filter(el => el._id === "5f3aadd5d756e9341ef74e2b")[0]
// this is the element filtered by _id
console.log(elementToUpdate)
//lets say you want to update rawMaterial
elementToUpdate.rawMaterial += ", Egg black 2"
console.log(elementToUpdate)
// this is the input object modified
console.log(input)
Related
I have searched a lot regarding filter of product data using checkbox but couldn't able to find the right answer for my issue.
Problem: I'm making a product filter page using checkbox in react but could not found the proper way to filter the true value of object key.
ie. I have to only filter object key value === true. Below array object is after checkbox selection before the selection it will be falsy value.
const category = [
{
"type":"Color",
"options":[
{
"key":"Red",
"value":true
},
{
"key":"Blue",
"value":false
},
{
"key":"Green",
"value":true
}
]
},
{
"type":"Gender",
"options":[
{
"key":"Men",
"value":true
},
{
"key":"Women",
"value":false
}
]
},
{
"type":"Price",
"options":[
{
"key":"0 - Rs. 250",
"from":0,
"to":250,
"value":false
},
{
"key":"Rs. 251 - 450",
"from":251,
"to":450,
"value":true
},
{
"key":"Rs. 451 & above",
"from":451,
"to":"Number.MAX_VALUE",
"value":false
}
]
},
{
"type":"Type",
"options":[
{
"key":"Polo",
"value":false
},
{
"key":"Hoodie",
"value":false
},
{
"key":"Basic",
"value":true
}
]
}
]
const productData = [
{
"id": 1,
"name": "Black Polo",
"type": "Polo",
"price": 250,
"currency": "INR",
"color": "Black",
"gender": "Men",
"quantity": 3
},
{
"id": 2,
"name": "Blue Polo",
"type": "Polo",
"price": 350,
"currency": "INR",
"color": "Blue",
"gender": "Women",
"quantity": 3
},
{
"id": 3,
"name": "Pink Polo",
"type": "Polo",
"price": 350,
"currency": "INR",
"color": "Pink",
"gender": "Women",
"quantity": 6
},
{
"id": 4,
"name": "Black Hoodie",
"type": "Hoodie",
"price": 500,
"currency": "INR",
"color": "Black",
"gender": "Men",
"quantity": 2
},
{
"id": 5,
"name": "Green Polo",
"type": "Polo",
"price": 250,
"currency": "INR",
"color": "Green",
"gender": "Men",
"quantity": 1
},
{
"id": 6,
"name": "Green Polo",
"type": "Polo",
"price": 350,
"currency": "INR",
"color": "Green",
"gender": "Women",
"quantity": 1
},
{
"id": 7,
"name": "Blue Hoodie",
"type": "Hoodie",
"price": 500,
"currency": "INR",
"color": "Blue",
"gender": "Women",
"quantity": 2
},
{
"id": 8,
"name": "Black Hoodie",
"type": "Hoodie",
"price": 500,
"currency": "INR",
"color": "Black",
"gender": "Women",
"quantity": 5
}]
Now,I need filtered product data from the above category data and product-data.
Here's my Codesandbox link for this.
I have an array that i need to treat to extract and form a new object, this is the code i run right now:
data_proces = Object.values(selectedData.reduce((r, { Code, Description, Price}) => {
r[Description] ??= { Code, Description, Units: 0, Price: 0 , Total: 0};
r[Description].Code= Code;
r[Description].Units++;
r[Description].Price = Price ;
r[Description].Total += Price ;
return r;
}, {}));
This gives me:
[{
"Code": 0,
"Description": "No Factured Act",
"Units": 2,
"Price": 0,
"Total": 0
},
{
"Code": 1,
"Description": "Autopsy",
"Units": 1,
"Price": 44,
"Total": 44
},
{
"Code": 2,
"Description": "Simple Biopsy",
"Units": 3,
"Price": 29,
"Total": 87
},
{
"Code": 1,
"Description": "Citology",
"Units": 4,
"Price": 15,
"Total": 60
},
{
"Code": " -",
"Description": "Free Act",
"Units": 2,
"Price": 56789,
"Total": 91356
}]
And this is the result i want:
[{
"Code": 0,
"Description": "No Factured Act",
"Units": 2,
"Price": 0,
"Total": 0
},
"Code": 0,
"Description": "No Factured Act",
"Units": 1,
"Price": 0,
"Total": 0
},
{
"Code": 1,
"Description": "Autopsy",
"Units": 1,
"Price": 44,
"Total": 44
},
{
"Code": 2,
"Description": "Simple Biopsy",
"Units": 3,
"Price": 29,
"Total": 87
},
{
"Code": 1,
"Description": "Citology",
"Units": 4,
"Price": 15,
"Total": 60
},
{
"Code": " -",
"Description": "Free Act",
"Units": 1,
"Price": 34567,
"Total": 34567
},
{
"Code": " -",
"Description": "Free Act",
"Units": 1,
"Price": 56789,
"Total": 56789
}]
As you can see, i need "No Factured Act" and "Free Act" to NOT sum up their units and stay as an individual values, how can i achieve this with Reduce?.
You can simply create an array of Description properties that you don't want summed and create unique keys if the iterated description is included in the array. Here using the third index parameter of the reduce() callback.
const selectedData = [{ "Code": 0, "Description": "No Factured Act", "Price": 0, }, { "Code": 0, "Description": "No Factured Act", "Price": 0, }, { "Code": 1, "Description": "Autopsy", "Price": 44, }, { "Code": 2, "Description": "Simple Biopsy", "Price": 29, }, { "Code": 2, "Description": "Simple Biopsy", "Price": 29, }, { "Code": 2, "Description": "Simple Biopsy", "Price": 29, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": " -", "Description": "Free Act", "Price": 34567, }, { "Code": " -", "Description": "Free Act", "Price": 56789, }];
const noSum = ['No Factured Act', 'Free Act'];
const data_process = Object.values(
selectedData.reduce((r, { Code, Description, Price }, i) => {
let key = Description;
if (noSum.includes(key)) {
key = `${key}_${i}`;
}
r[key] ??= { Code, Description, Units: 0, Price, Total: 0 };
r[key].Units++;
r[key].Total += Price;
return r;
}, {})
);
console.log(data_process);
I have used Angular as programming language for my project and i am a beginner. But i am stuck at the point where i have to generate PDF of my JSON response.
Looked at the documentation of PDFMAKE to achieve the same, but couldnot figure out how to implement it and come out with the output as shown in following image.
Output Structure Image
The JSON response is as follows:
{
"statusCode": true,
"data": [
{
"partyId": 1,
"partyIdDisp": "1/PA/2077",
"createdDate": "2077/05/03",
"partyName": "Roshan Basnet",
"contactNumber": 9841040791,
"companyName": "Butwal Poultry",
"panNumber": 321654,
"address": "Butwal, Nepal",
"saleDetails": [
{
"_id": "5f5b234cbac67e377c2a700f",
"saleId": 1,
"saleIdDisp": "1/S/2077",
"createdDate": "2077/05/26",
"saleType": "Credit",
"customerId": 1,
"customerName": "Roshan Basnet",
"contactNumber": 9841040791,
"billingName": "Butwal Poultry",
"billingAddress": "Butwal, Nepal",
"billDate": "2077/05/26",
"itemDetails": [
{
"_id": "5f5b234cbac67e377c2a7010",
"itemId": 1,
"itemName": "Chicken",
"quantity": 10,
"unit": "KG",
"piece": 5,
"receiptNo": null,
"billNo": 366,
"unitPrice": 310,
"itemAmount": 3100
}
],
"totalQuantity": 10,
"paymentMethod": "Cash",
"subTotal": 3100,
"amountInWords": "three thousand one hundred only",
"outstandingBalance": 0,
"receivedAmount": 3000,
"balanceAmount": 100,
"__v": 0
},
{
"_id": "5f5b2375bac67e377c2a7012",
"saleId": 2,
"saleIdDisp": "2/S/2077",
"createdDate": "2077/05/26",
"saleType": "Credit",
"customerId": 1,
"customerName": "Roshan Basnet",
"contactNumber": 9841040791,
"billingName": "Butwal Poultry",
"billingAddress": "Butwal, Nepal",
"billDate": "2077/05/26",
"itemDetails": [
{
"_id": "5f5b2375bac67e377c2a7013",
"itemId": 2,
"itemName": "Mutton",
"quantity": 5,
"unit": "KG",
"piece": 0,
"receiptNo": null,
"billNo": 266,
"unitPrice": 1200,
"itemAmount": 6000
},
{
"_id": "5f5b2375bac67e377c2a7014",
"itemId": 3,
"itemName": "Eggs",
"quantity": 10,
"unit": "Crate",
"piece": 0,
"receiptNo": null,
"billNo": 366,
"unitPrice": 390,
"itemAmount": 3900
}
],
"totalQuantity": 15,
"paymentMethod": "Cash",
"subTotal": 10000,
"amountInWords": "ten thousand only",
"outstandingBalance": 100,
"receivedAmount": 5000,
"balanceAmount": 5000,
"__v": 0
}
]
}
]
}
A code snippet how to generate a pdf output as per the structure in the image would be helpful.
I have two arrays, BasicItem1 & BasicItem2, I want to update the rate in details array of BasicItem1 with the rate of items in details array of BasicItem2.
For Example change rate of "Almond Flour" in BasicItem 1 from "1350" to "1250" and for "Egg White 1" from "15" to "225"
BasicItems1:
[{
"_id": "5f4249d613ed7a94355aac67",
"name": "New Macaron sshell",
"details": [
{
"_id": "5f397abb59ad0ba71f27fa93",
"name": "Egg white 1",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 15,
"quantityInRecipe": 121
},
{
"_id": "5f397adb59ad0ba71f27fa94",
"name": "Egg white 2",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 18,
"quantityInRecipe": 121,
},
{
"_id": "5f36cdfc875da6156c9a078f",
"name": "Almound Flour",
"baseQuantity": 1000,
"baseUnit": "gm",
"rate": "1350",
"quantityInRecipe": "350",
}
]
}]
BasicItem2
[{
"_id": "5f4249d613ed7a94355aac67",
"name": "New Macaron sshell",
"details": [
{
"_id": "5f397abb59ad0ba71f27fa93",
"name": "Egg white 1",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 225,
"quantityInRecipe": 121
},
{
"_id": "5f397adb59ad0ba71f27fa94",
"name": "Egg white 2",
"baseQuantity": 100,
"baseUnit": "gm",
"rate": 18,
"quantityInRecipe": 121,
},
{
"_id": "5f36cdfc875da6156c9a078f",
"name": "Almound Flour",
"baseQuantity": 1000,
"baseUnit": "gm",
"rate": "1250",
"quantityInRecipe": "350",
}
]
}]
This will do it
let basicItem1Details = basicItem1[0].details
let basicItem2Details = basicItem2[0].details
basicItem1Details.forEach((detail, index) => {
if (detail._id === basicItem2Details[index]._id)
detail.rate = basicItem2Details[index].rate
})
I am trying to output parts of the json below:
How would I output the value pint from beer.name.568.name ?
{
"beer": {
"name": "Beer or cider",
"sizes": {
"568": {
"name": "Pint",
"size": 0.568,
"id": "pint",
"max": 10,
"icon": "beer_pint"
},
"440": {
"name": "Can",
"size": 0.44,
"id": "can",
"max": 10,
"icon": "beer_can"
},
"330": {
"name": "Bottle",
"size": "0.33",
"id": "bottle",
"max": "10",
"icon": "beer_bottle_330"
},
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "smallBottle",
"max": 10,
"icon": "beer_bottle_275"
}
},
"strength": [4, 4.5, 5, 6, 7, 8, 9]
},
"wine": {
"name": "Wine or champagne",
"sizes": {
"125": {
"name": "Small glass",
"size": 0.125,
"id": "small",
"max": 10,
"icon": "wine_small_glass"
},
"175": {
"name": "Standard glass",
"size": 0.175,
"id": "std",
"max": 10,
"icon": "wine_standard_glass"
},
"250": {
"name": "Large glass",
"size": 0.25,
"id": "large",
"max": 10,
"icon": "wine_large_glass"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 10,
"icon": "wine_bottle"
}
},
"strength": [9, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17]
},
"spirits": {
"name": "Spirits or shots",
"sizes": {
"25": {
"name": "Single",
"size": 0.025,
"id": "single",
"max": 10,
"icon": "spirit_single"
},
"35": {
"name": "Large single",
"size": 0.035,
"id": "lgSingle",
"max": 10,
"icon": "spirit_large_single"
},
"50": {
"name": "Double",
"size": 0.05,
"id": "double",
"max": 10,
"icon": "spirit_double"
},
"70": {
"name": "Large double",
"size": 0.07,
"id": "lgDouble",
"max": 10,
"icon": "spirit_large_double"
},
"700": {
"name": "Bottle",
"size": 0.7,
"id": "bottle700",
"max": 3,
"icon": "spirit_bottles"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 3,
"icon": "spirit_bottles"
}
},
"strength": [37, 40]
},
"alcopop": {
"name": "Alcopop",
"sizes": {
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "small",
"max": 10,
"icon": "alcopops_small_bottle"
},
"330": {
"name": "Standard bottle",
"size": 0.33,
"id": "std",
"max": 10,
"icon": "alcopops_standard_bottle"
},
"750": {
"name": "Large bottle",
"size": 0.75,
"id": "large",
"max": 10,
"icon": "alcopops_large_bottle"
}
},
"strength": [5, 5.5]
}
}
Obviously this approach may have many drawbacks as the loop can go infinite in some situations, but, if you know that your structure is "stable", this can simulate a search in some dynamic json schema, when you know a key and want to get a property value.
var data = {
"beer": {
"name": "Beer or cider",
"sizes": {
"568": {
"name": "Pint",
"size": 0.568,
"id": "pint",
"max": 10,
"icon": "beer_pint"
},
"440": {
"name": "Can",
"size": 0.44,
"id": "can",
"max": 10,
"icon": "beer_can"
},
"330": {
"name": "Bottle",
"size": "0.33",
"id": "bottle",
"max": "10",
"icon": "beer_bottle_330"
},
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "smallBottle",
"max": 10,
"icon": "beer_bottle_275"
}
},
"strength": [4, 4.5, 5, 6, 7, 8, 9]
},
"wine": {
"name": "Wine or champagne",
"sizes": {
"125": {
"name": "Small glass",
"size": 0.125,
"id": "small",
"max": 10,
"icon": "wine_small_glass"
},
"175": {
"name": "Standard glass",
"size": 0.175,
"id": "std",
"max": 10,
"icon": "wine_standard_glass"
},
"250": {
"name": "Large glass",
"size": 0.25,
"id": "large",
"max": 10,
"icon": "wine_large_glass"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 10,
"icon": "wine_bottle"
}
},
"strength": [9, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17]
},
"spirits": {
"name": "Spirits or shots",
"sizes": {
"25": {
"name": "Single",
"size": 0.025,
"id": "single",
"max": 10,
"icon": "spirit_single"
},
"35": {
"name": "Large single",
"size": 0.035,
"id": "lgSingle",
"max": 10,
"icon": "spirit_large_single"
},
"50": {
"name": "Double",
"size": 0.05,
"id": "double",
"max": 10,
"icon": "spirit_double"
},
"70": {
"name": "Large double",
"size": 0.07,
"id": "lgDouble",
"max": 10,
"icon": "spirit_large_double"
},
"700": {
"name": "Bottle",
"size": 0.7,
"id": "bottle700",
"max": 3,
"icon": "spirit_bottles"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 3,
"icon": "spirit_bottles"
}
},
"strength": [37, 40]
},
"alcopop": {
"name": "Alcopop",
"sizes": {
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "small",
"max": 10,
"icon": "alcopops_small_bottle"
},
"330": {
"name": "Standard bottle",
"size": 0.33,
"id": "std",
"max": 10,
"icon": "alcopops_standard_bottle"
},
"750": {
"name": "Large bottle",
"size": 0.75,
"id": "large",
"max": 10,
"icon": "alcopops_large_bottle"
}
},
"strength": [5, 5.5]
}
};
function findPropertyValueForKey(prev, key, property, data) {
if (typeof data != "object") return;
var keys = Object.keys(data);
for (var i = 0; i < keys.length; i++) {
if (keys[i] == key) {
prev.push(data[key][property]);
} else {
findPropertyValueForKey(prev, key, property, data[keys[i]]);
}
}
};
var results = [];
findPropertyValueForKey(results, "568", "name", data);
console.log(results);
var beer = {
"sizes": {
"568": {
"name": "Pint"
}
}
}
console.log(beer.sizes[568].name); // "Pint"
console.log(beer['sizes'][568]['name']); // "Pint"