Sort Nested Javascript - javascript

const list= [
{
id: 1,
amount: 20,
dueDate: '2021-05-01',
splits: [{ id: 1, amount: 15, dueDate: '2021-09-01' },{ id: 2,
amount: 5, dueDate: '2021-03-01' }],
},
{
id: 2,
amount: 45,
dueDate: '2021-07-01',
splits: [{ id: 1, amount: 20, dueDate: '2021-11-01' },{ id: 2,
amount: 24, dueDate: '2021-09-01' },{ id: 3, amount: 1, dueDate:
'2021-10-01' }],
},
{ id: 3, amount: 10, dueDate: '2021-08-01' },
{ id: 4, amount: 30, dueDate: '2021-06-01', splits: [{ id: 1, amount: 30, dueDate: '2021-01-01' }] },
{ id: 5, amount: 15, dueDate: '2021-04-01' },
];
I want to write function that sorts a list of invoices by due date (ascending) and returns a list of object containing the invoice id with below conditions:
1- For invoices with splits, sort by earliest split's due date,or invoice 1
(id: 1) the earliest would be split 2 (id: 2).
2- For invoices without splits, the earliest dueDate is the top-level due date.
Expected sorted invoices: [{"id": 4}, {"id": 1}, {"id": 5}, {"id": 3}, {"id": 2}]

function sortFun(a,b){
if(a.splits){
a = a.splits.sort(sortFun)[0]
}
if(b.splits){
b = b.splits.sort(sortFun)[0]
}
return new Date(a.dueDate) - new Date(b.dueDate)
}
let result = list.sort(sortFun).map(x=>({id:x.id}));
console.log(result)
const list = [{
id: 1,
amount: 20,
dueDate: '2021-05-01',
splits: [{
id: 1,
amount: 15,
dueDate: '2021-09-01'
}, {
id: 2,
amount: 5,
dueDate: '2021-03-01'
}],
},
{
id: 2,
amount: 45,
dueDate: '2021-07-01',
splits: [{
id: 1,
amount: 20,
dueDate: '2021-11-01'
}, {
id: 2,
amount: 24,
dueDate: '2021-09-01'
}, {
id: 3,
amount: 1,
dueDate: '2021-10-01'
}],
},
{
id: 3,
amount: 10,
dueDate: '2021-08-01'
},
{
id: 4,
amount: 30,
dueDate: '2021-06-01',
splits: [{
id: 1,
amount: 30,
dueDate: '2021-01-01'
}]
},
{
id: 5,
amount: 15,
dueDate: '2021-04-01'
},
];
function sortFun(a,b){
if(a.splits){
a = a.splits.sort(sortFun)[0]
}
if(b.splits){
b = b.splits.sort(sortFun)[0]
}
return new Date(a.dueDate) - new Date(b.dueDate)
}
let result = list.sort(sortFun).map(x=>({id:x.id}));
console.log(result)

You could do something like:
list.sort((prev, next) => {
let prevDueDate = prev.splits ? prev.splits.map(el => el.dueDate).sort((a, b) => a.localeCompare(b))[0] : prev.dueDate;
let nextDueDate = next.splits ? next.splits.map(el => el.dueDate).sort((a, b) => a.localeCompare(b))[0] : next.dueDate;
return prevDueDate.localeCompare(nextDueDate);
}).map(el => ({ id: el.id }));
See these references for more help:
MDN - Array.prototype.sort()
MDN - String.prototype.localeCompare()
const list= [
{
id: 1,
amount: 20,
dueDate: '2021-05-01',
splits: [{ id: 1, amount: 15, dueDate: '2021-09-01' },{ id: 2,
amount: 5, dueDate: '2021-03-01' }],
},
{
id: 2,
amount: 45,
dueDate: '2021-07-01',
splits: [{ id: 1, amount: 20, dueDate: '2021-11-01' },{ id: 2,
amount: 24, dueDate: '2021-09-01' },{ id: 3, amount: 1, dueDate:
'2021-10-01' }],
},
{ id: 3, amount: 10, dueDate: '2021-08-01' },
{ id: 4, amount: 30, dueDate: '2021-06-01', splits: [{ id: 1, amount: 30, dueDate: '2021-01-01' }] },
{ id: 5, amount: 15, dueDate: '2021-04-01' },
];
let res = list.sort((prev, next) => {
let prevDueDate = prev.splits ? prev.splits.map(el => el.dueDate).sort((a, b) => a.localeCompare(b))[0] : prev.dueDate;
let nextDueDate = next.splits ? next.splits.map(el => el.dueDate).sort((a, b) => a.localeCompare(b))[0] : next.dueDate;
return prevDueDate.localeCompare(nextDueDate);
}).map(el => ({ id: el.id }));
console.log(res);

Hope this is the solution that you are looking for.
const list = [
{
id: 1,
amount: 20,
dueDate: '2021-05-01',
splits: [
{ id: 1, amount: 15, dueDate: '2021-09-01' },
{ id: 2, amount: 5, dueDate: '2021-03-01' }
],
},
{
id: 2,
amount: 45,
dueDate: '2021-07-01',
splits: [
{ id: 1, amount: 20, dueDate: '2021-11-01' },
{ id: 2, amount: 24, dueDate: '2021-09-01' },
{ id: 3, amount: 1, dueDate: '2021-10-01' }
],
},
{
id: 3,
amount: 10,
dueDate: '2021-08-01'
},
{
id: 4,
amount: 30,
dueDate: '2021-06-01',
splits: [{ id: 1, amount: 30, dueDate: '2021-01-01' }]
},
{
id: 5,
amount: 15,
dueDate: '2021-04-01'
},
];
const getDateForObj = (obj) => {
if (obj.splits) {
const dateList = obj.splits.map((split) => split.dueDate);
const smallestDate = dateList.sort((a, b) => new Date(a) - new Date(b))[0];
return smallestDate;
} else {
return obj.dueDate
}
}
const newList = list.sort((a, b) =>{
const aDate = getDateForObj(a);
const bDate = getDateForObj(b);
return new Date(aDate) - new Date(bDate);
});
console.log(newList);

Related

Question from a beginner: Unexpected JS behavior [duplicate]

I'm trying to convert an array of objects where i return duplicated objects if the object properties quantity is greater than 1.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
// desired return
[
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 2, name: "Hat", price: 6.5}
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
]
My code:
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects= [];
Object.entries(objects).forEach(([key, value]) => {
for (let i=0; i < value.quantity; i++){
newObjects.push({ id: value.id, name: value.name, price: value.price})
}
});
console.log(newObjects);
So my code above does work, does return what i wanted, however i feel like there is a better/smoother and more of ES6 and beyond method. Could anyone please suggest a better way?
You could use .fill() and .flatMap().
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects = objects.flatMap(e=>
Array(e.quantity).fill({id: e.id, name: e.name, price: e.price})
);
console.log(newObjects);
You can use an array reduce along with an array fill.
The map is required only if you want to have unique references otherwise you can fill using the same object.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
const output = objects.reduce((a, c) => {
return a.concat(Array(c.quantity).fill({}).map(x=>({
id: c.id,
name: c.name,
price: c.price
})))
}, []);
console.log(output)

Using reduce to get sum of an object

I want to have a sum of all the particulars which are in selectedIds array.
return [
{
id: 1244,
name: "Installment 1",
amount: 10000,
due_date: new Date(),
particulars: [
{
id: 2415123,
name: "Development Fee",
amount: 5000,
},
{
id: 14123,
name: "Library Fee",
amount: 3000,
},
{
id: 5123151,
name: "Sports Fee",
amount: 2000,
},
]
},
{
id: 1412,
name: "Installment 2",
amount: 6000,
due_date: new Date(),
particulars: [
{
id: 414,
name: "Development Fee",
amount: 5000,
},
{
id: 5123,
name: "Library Fee",
amount: 3000,
},
{
id: 515151,
name: "Sports Fee",
amount: 2000,
},
]
const selectedIds = [14123, 414];
In the object, I want to have a sum of all the particulars which are in selectedIds array.
I was trying to use array function to get the result. This is what I could come up with.
const selectedInstallments = this.studentInstallments
.filter((installment) =>
installment.particulars.some((particular) =>
this.selectedParticulars.includes(particular.id)
)
)
.map((installment) => installment.particulars);
console.log(selectedInstallments);
const sumParticularReducer = (acc, current) => {
return acc;
};
I couldn't figure out how to use reduce to get the result.
Please try this code.
const selectedIds = [14123, 414];
const sum = arr.reduce((prev, cur) => {
return prev + cur.particulars.reduce((old, item) => (
selectedIds.indexOf(item.id) >= 0 ? old + item.amount : old
), 0);
}, 0);
console.log(sum);
Something like:
const selectedIds = [14123, 414];
const idsArray = [{
id: 1244,
name: "Installment 1",
amount: 10000,
due_date: new Date(),
particulars: [{
id: 2415123,
name: "Development Fee",
amount: 5000,
},
{
id: 14123,
name: "Library Fee",
amount: 3000,
},
{
id: 5123151,
name: "Sports Fee",
amount: 2000,
},
]
},
{
id: 1412,
name: "Installment 2",
amount: 6000,
due_date: new Date(),
particulars: [{
id: 414,
name: "Development Fee",
amount: 5000,
},
{
id: 5123,
name: "Library Fee",
amount: 3000,
},
{
id: 515151,
name: "Sports Fee",
amount: 2000,
},
]
}
]
const sum = idsArray.reduce((sum, value) => sum + value.particulars.reduce(
(partSum, partValue) => selectedIds.includes(partValue.id) ? (partSum + partValue.amount) : partSum, 0),
0
);
console.log(sum);
const data = [{
id: 1244,
name: "Installment 1",
amount: 10000,
due_date: new Date(),
particulars: [{
id: 2415123,
name: "Development Fee",
amount: 5000,
},
{
id: 14123,
name: "Library Fee",
amount: 3000,
},
{
id: 5123151,
name: "Sports Fee",
amount: 2000,
},
]
},
{
id: 1412,
name: "Installment 2",
amount: 6000,
due_date: new Date(),
particulars: [{
id: 414,
name: "Development Fee",
amount: 5000,
},
{
id: 5123,
name: "Library Fee",
amount: 3000,
},
{
id: 515151,
name: "Sports Fee",
amount: 2000,
},
]
}
]
const selectedIds = [14123, 414];
const result = selectedIds.reduce(
(acc, id) => acc + data.reduce(
(acc, { particulars }) =>
acc + (particulars.find(p => p.id === id)?.amount || 0)
, 0)
, 0)
console.log(result)

How would I prevent duplicates in array of objects? And if there is a object with same property, it should sume its value

So basically what I am trying to achieve is.. I have an array with objects, in my case array of items in stock. And when I add item to cart array, it should check if an object with the same property (name) already exists. And if it exists, it should just sum the count of them. So that I don't have duplicates... But it sums the count.
const stock = [
{
id: 0,
name: 'Leaf Blower',
price: 250,
stock: 24,
count: 0,
logoURL: process.env.PUBLIC_URL + '/images/1.png',
},
{
id: 1,
name: 'Generator',
price: 1299,
stock: 12,
count: 0,
logoURL: process.env.PUBLIC_URL + '/images/2.png',
},
{
id: 2,
name: 'Log Splitter',
price: 2133,
stock: 5,
count: 0,
logoURL: process.env.PUBLIC_URL + '/images/3.png',
},
{
id: 3,
name: 'Lawn Mower',
price: 250,
stock: 24,
count: 0,
logoURL: process.env.PUBLIC_URL + '/images/4.png',
},
{
id: 4,
name: 'Chainsaw',
price: 344,
stock: 16,
count: 0,
logoURL: process.env.PUBLIC_URL + '/images/5.png',
},
];
This:
const shoppingCart = [
{
id: 0,
name: 'Leaf Blower',
price: 250,
stock: 24,
count: 5,
logoURL: process.env.PUBLIC_URL + '/images/1.png',
},
id: 0,
name: 'Leaf Blower',
price: 250,
stock: 24,
count: 3,
logoURL: process.env.PUBLIC_URL + '/images/1.png',
}
]
Should be:
const shoppingCart = [
{
id: 0,
name: 'Leaf Blower',
price: 250,
stock: 24,
count: 8,
logoURL: process.env.PUBLIC_URL + '/images/1.png',
}
]
You can check if the objectToBeAdded is already there in the shoppingCart. And then update shoppingCart accordingly.
const indexOfObject = shoppingCart.findIndex(item => item.name === objectToBeAdded.name);
if(indexOfObject > -1) {
shoppingCart[indexOfObject].count += objectToBeAdded.count;
} else {
shoppingCart = [...shoppingCart, objectToBeAdded];
}
You can do this by using array reduce method. Just traverse the array and sum the count property.
const shoppingCart = [
{
id: 0,
name: 'Leaf Blower',
price: 250,
stock: 24,
count: 5,
logoURL: `process.env.PUBLIC_URL + '/images/1.png`,
},
{
id: 0,
name: 'Leaf Blower',
price: 250,
stock: 24,
count: 3,
logoURL: `process.env.PUBLIC_URL + '/images/1.png`,
},
];
let ret = shoppingCart.reduce((prev, c) => {
const p = prev;
if (!p[c.id]) p[c.id] = { ...c };
else p[c.id] = { ...c, count: c.count + p[c.id].count };
return p;
}, {});
ret = Object.values(ret);
console.log(ret);
Something like this should work:
if (!shoppingCart.find(element => element.name == objectToBeAdded.name)) {shoppingCart.push(objectToBeAdded)}
Here, you use Array.find() to check if there is an object with a matching name in the array. If not, you add the object to array.

How to add key in a nested array of Objects based on another object

I have an array of Objects named results and have the Object which key is the key value of editKeyValues object and value needs to be add in edit key. Below is the Input
var result = [{
name : 'database',
checked : true,
key : 1,
schemas : [
{
name : "schema2",
checked : true,
key : 6,
tables : [
{
name : "table2",
checked : true,
key : 7,
columns : [
{
name : "column1",
checked : true,
key : 8,
}
]
},
]
},
]
}]
var editKeyValues = { 8 : "column4", 6 : "schema4"}
Output that i want in a below format
var result = [{
name : 'database',
checked : true,
key : 1,
schemas : [
{
name : "schema2",
checked : true,
key : 6,
edit : "schema4"
tables : [
{
name : "table2",
checked : true,
key : 7,
columns : [
{
name : "column1",
checked : true,
key : 8,
edit : "column4" // value of key 8 from editkeyValues array
}
]
},
]
},]}]
Provide me a best approach because there is large amount of data in results Object... Is recursion a good idea ??? Help me to find out the best approach.
let data = [{
name: 'database',
checked: true,
key: 1,
schemas: [
{
name: "schema2",
checked: true,
key: 6,
tables: [
{
name: "table2",
checked: true,
key: 7,
columns: [
{
name: "column1",
checked: true,
key: 8,
}
]
},
]
},
]
}];
let schemas = data.flatMap(d => d.schemas);
let tables = schemas.flatMap(s => s.tables);
let columns = tables.flatMap(t => t.columns);
let flatData = [data, schemas, tables, columns].flat();
let editKeyValues = {8: "column4", 6: "schema4"};
Object.entries(editKeyValues).forEach(([key, newName]) =>
flatData.find(obj => obj.key === parseInt(key)).edit = newName);
console.log(data);
const data = [
{
id: 13,
product_name: 'Onion Pizza',
image: '26238.jpg',
category: {
id: 1,
name: 'Restaurants',
image: 'restaurant.png',
created_at: '2022-02-02T07:59:05.000000Z',
updated_at: '2022-02-15T10:58:49.000000Z',
},
status: '1',
created_at: '2022-02-19T12:22:09.000000Z',
updated_at: '2022-03-02T09:43:53.000000Z',
add_in_cart: false,
quantity_in_cart: 0,
add_in_wishlist: false,
variant: [
{
id: 34,
price: 140,
quantity: 10,
weight: '7 inch',
discount: 20,
product_id: 13,
created_at: '2022-03-02T12:46:06.000000Z',
updated_at: '2022-03-02T12:46:06.000000Z',
},
{
id: 35,
price: 350,
quantity: 5,
weight: '14 inch',
discount: 20,
product_id: 13,
created_at: '2022-03-02T12:46:06.000000Z',
updated_at: '2022-03-02T12:46:06.000000Z',
},
],
},
{
id: 15,
product_name: 'Veg Fresh Pizza',
image: '816404.jpg',
category: {
id: 1,
name: 'Restaurants',
image: 'restaurant.png',
created_at: '2022-02-02T07:59:05.000000Z',
updated_at: '2022-02-15T10:58:49.000000Z',
},
status: '1',
created_at: '2022-03-01T12:40:15.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
add_in_cart: false,
quantity_in_cart: 0,
add_in_wishlist: false,
variant: [
{
id: 30,
price: 129,
quantity: 1,
weight: '7 inch',
discount: 20,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
{
id: 31,
price: 200,
quantity: 1,
weight: '12 inch',
discount: 25,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
{
id: 32,
price: 500,
quantity: 1,
weight: '18 inch',
discount: 40,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
{
id: 33,
price: 700,
quantity: 1,
weight: '20 inch',
discount: 30,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
],
},
];
let i = 0;
let j = 0;
for (i; i < data.length; i++) {
for (j; j < data[i].variant.length; j++) {
data[i].variant.forEach(e => {
e.myKey = 0;
});
}
}
console.log('data : ' + JSON.stringify(data));
const data = [
{
id: 13,
product_name: 'Onion Pizza',
image: '26238.jpg',
category: {
id: 1,
name: 'Restaurants',
image: 'restaurant.png',
created_at: '2022-02-02T07:59:05.000000Z',
updated_at: '2022-02-15T10:58:49.000000Z',
},
status: '1',
created_at: '2022-02-19T12:22:09.000000Z',
updated_at: '2022-03-02T09:43:53.000000Z',
add_in_cart: false,
quantity_in_cart: 0,
add_in_wishlist: false,
variant: [
{
id: 34,
price: 140,
quantity: 10,
weight: '7 inch',
discount: 20,
product_id: 13,
created_at: '2022-03-02T12:46:06.000000Z',
updated_at: '2022-03-02T12:46:06.000000Z',
},
{
id: 35,
price: 350,
quantity: 5,
weight: '14 inch',
discount: 20,
product_id: 13,
created_at: '2022-03-02T12:46:06.000000Z',
updated_at: '2022-03-02T12:46:06.000000Z',
},
],
},
{
id: 15,
product_name: 'Veg Fresh Pizza',
image: '816404.jpg',
category: {
id: 1,
name: 'Restaurants',
image: 'restaurant.png',
created_at: '2022-02-02T07:59:05.000000Z',
updated_at: '2022-02-15T10:58:49.000000Z',
},
status: '1',
created_at: '2022-03-01T12:40:15.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
add_in_cart: false,
quantity_in_cart: 0,
add_in_wishlist: false,
variant: [
{
id: 30,
price: 129,
quantity: 1,
weight: '7 inch',
discount: 20,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
{
id: 31,
price: 200,
quantity: 1,
weight: '12 inch',
discount: 25,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
{
id: 32,
price: 500,
quantity: 1,
weight: '18 inch',
discount: 40,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
{
id: 33,
price: 700,
quantity: 1,
weight: '20 inch',
discount: 30,
product_id: 15,
created_at: '2022-03-02T12:27:28.000000Z',
updated_at: '2022-03-02T12:27:28.000000Z',
},
],
},
];
let i = 0;
let j = 0;
for (i; i < data.length; i++) {
for (j; j < data[i].variant.length; j++) {
data[i].variant.forEach(e => {
e.myKey = 0;
});
}
}
console.log('data : ' + JSON.stringify(data));
You could take a Map and iterate the objects and seach for nested array.
This approach uses a short circuit if no more nodes are eligible for update.
function update(array, nodes) {
return array.some(o => {
var key = o.key.toString();
if (nodes.has(key)) {
o.edit = nodes.get(key);
nodes.delete(key);
if (!nodes.size) return true;
}
return update(Object.values(o).find(Array.isArray) || [], nodes);
});
}
var data = [{ name: 'database', checked: true, key: 1, schemas: [{ name: "schema1", checked: true, key: 2, tables: [{ name: "table1", checked: true, key: 3, columns: [{ name: "column2", checked: true, key: 5 }] }] }, { name: "schema2", checked: true, key: 6, tables: [{ name: "table2", checked: true, key: 7, columns: [{ name: "column1", checked: true, key: 8 }] }] }] }],
keyValues = { 8: 'column4', 6: 'schema4' };
update(data, new Map(Object.entries(keyValues)));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Filter and combine fields from 2 arrays with ES6 JavaScript?

I have 2 arrays, one of pizza details and the other is an order state. The id field is what links them. So in this example, the order has 2 x Pepperoni and 3 x Margarita.
const pizzaContent = [
{
id: 0,
name: 'Pepperoni',
price: 20,
hot: true,
stockQuantity: 3
},
{
id: 1,
name: 'Margarita',
price: 25,
stockQuantity: 3
},
{
id: 2,
name: 'Hawaiian',
price: 15,
stockQuantity: 0
}
];
const orders = [{
id: 0,
quantity: 2
},{
id: 1,
quantity: 3
}];
I'm trying to create a new array which has the quantity from orders and the fields from pizzaContent. Any pizzas which aren't in the order shouldn't be part of this array.
I've gotten close with the following:
const pizzasInOrder = this.props.orders.map(order => {
return (
{
quantity: order.quantity,
pizza: this.props.pizzas.find(pizza => {
return (
order.id === pizza.id
);
})
}
)
});
However, the result is:
pizzasInOrder = [
{
pizza: {id: 0, name: "Pepperoni", price: 20, hot: true, stockQuantity: 3},
quantity:2
},
{
pizza: {id: 1, name: "Margarita", price: 25, stockQuantity: 3},
quantity:3
}
]
But what I need is:
pizzasInOrder = [
{
id: 0, name: "Pepperoni", price: 20, hot: true, stockQuantity: 3, quantity: 2
},
{
id: 1, name: "Margarita", price: 25, stockQuantity: 3, quantity: 3
}
]
Use Object.assign and no extra keys
const pizzasInOrder = this.props.orders.map(order =>
Object.assign({quantity: order.quantity},
this.props.pizzas.find(pizza => order.id === pizza.id))
);
You can use Object.assign() to merge objects into one.
example..
const pizzaContent = [
{
id: 0,
name: 'Peperoni',
price: 20,
hot: true,
stockQuantity: 3
},
{
id: 1,
name: 'Margarita',
price: 25,
stockQuantity: 3
},
{
id: 2,
name: 'Hawian',
price: 15,
stockQuantity: 0
}
];
const orders = [{
id: 0,
quantity: 2
},{
id: 1,
quantity: 3
}];
let pizzasInOrder = orders.map((order) => {
return Object.assign(order,
pizzaContent.find(pizza => order.id === pizza.id));
});
console.log(pizzasInOrder);

Categories