Related
function main() {
let yesterdaysOrders = [
{
id: 1,
orderLines: [{
itemName: "Item 01",
quantity: 1
},
{
itemName: "Item 02",
quantity: 3
},
{
itemName: "Item 03",
quantity: 25
},
{
itemName: "Item 04",
quantity: 12
},
],
},
{
id: 2,
orderLines: [{
itemName: "Item 01",
quantity: 1
},
{
itemName: "Item 08",
quantity: 42
},
{
itemName: "Item 09",
quantity: 13
},
{
itemName: "Item 12",
quantity: 37
},
],
},
{
id: 3,
orderLines: [{
itemName: "Item 12",
quantity: 16
}, ],
},
{
id: 4,
orderLines: [{
itemName: "Item 10",
quantity: 11
},
{
itemName: "Item 11",
quantity: 10
},
],
},
{
id: 5,
orderLines: [{
itemName: "Item 06",
quantity: 7
},
{
itemName: "Item 07",
quantity: 2
},
{
itemName: "Item 12",
quantity: 14
},
],
},
{
id: 6,
orderLines: [{
itemName: "Item 05",
quantity: 17
}, ],
},
{
id: 7,
orderLines: [{
itemName: "Item 03",
quantity: 5
},
{
itemName: "Item 07",
quantity: 2
},
],
},
{
id: 8,
orderLines: [{
itemName: "Item 02",
quantity: 13
},
{
itemName: "Item 07",
quantity: 7
},
{
itemName: "Item 09",
quantity: 2
},
],
},
{
id: 9,
orderLines: [{
itemName: "Item 01",
quantity: 4
},
{
itemName: "Item 06",
quantity: 17
},
{
itemName: "Item 07",
quantity: 3
},
],
},
{
id: 10,
orderLines: [{
itemName: "Item 11",
quantity: 12
},
{
itemName: "Item 12",
quantity: 1
},
],
}
],
result = Array.from(
yesterdaysOrders.reduce((acc, {
orderLines
}) => {
orderLines.forEach(({
itemName,
quantity
}) => acc.set(itemName, (acc.get(itemName) || 0) + quantity));
return acc;
}, new Map), ([itemName, quantity]) => ({
itemName,
quantity
}));
}
I have this function and at the end, I'm grabbing the itemName and quantity so that eventually I can display them in the DOM.
I'm looking to refactor the result = Array.from.... to be used with a spread operator, along with the already implemented destructuring.
How would I go about starting this off?
I'm understanding that the spread operator allows anything that is iterable to be able to be expanded.
Also, within the main function, after I place the comma and define my new function with result, what is the explanation for not needing a var, let, or const before it?
I have a function that goes through and sorts items and their quantity and is supposed to display them in a table.
Nothing is appearing in the DOM or browser. Am I calling the function wrong or something?
I have an index.html file linked to my index.js file using a script.
There are no errors showing in chrome dev tools.
INSTRUCTION
Display the shelf and item pairings by calling the following
function displayShelfItemPair(shelfName, itemName);
function main() {
let yesterdaysOrders = [
{
id: 1,
orderLines: [{
itemName: "Item 01",
quantity: 1
},
{
itemName: "Item 02",
quantity: 3
},
{
itemName: "Item 03",
quantity: 25
},
{
itemName: "Item 04",
quantity: 12
},
],
},
{
id: 2,
orderLines: [{
itemName: "Item 01",
quantity: 1
},
{
itemName: "Item 08",
quantity: 42
},
{
itemName: "Item 09",
quantity: 13
},
{
itemName: "Item 12",
quantity: 37
},
],
},
{
id: 3,
orderLines: [{
itemName: "Item 12",
quantity: 16
}, ],
},
{
id: 4,
orderLines: [{
itemName: "Item 10",
quantity: 11
},
{
itemName: "Item 11",
quantity: 10
},
],
},
{
id: 5,
orderLines: [{
itemName: "Item 06",
quantity: 7
},
{
itemName: "Item 07",
quantity: 2
},
{
itemName: "Item 12",
quantity: 14
},
],
},
{
id: 6,
orderLines: [{
itemName: "Item 05",
quantity: 17
}, ],
},
{
id: 7,
orderLines: [{
itemName: "Item 03",
quantity: 5
},
{
itemName: "Item 07",
quantity: 2
},
],
},
{
id: 8,
orderLines: [{
itemName: "Item 02",
quantity: 13
},
{
itemName: "Item 07",
quantity: 7
},
{
itemName: "Item 09",
quantity: 2
},
],
},
{
id: 9,
orderLines: [{
itemName: "Item 01",
quantity: 4
},
{
itemName: "Item 06",
quantity: 17
},
{
itemName: "Item 07",
quantity: 3
},
],
},
{
id: 10,
orderLines: [{
itemName: "Item 11",
quantity: 12
},
{
itemName: "Item 12",
quantity: 1
},
],
}
],
result = Array.from(
yesterdaysOrders.reduce((acc, {
orderLines
}) => {
orderLines.forEach(({
itemName,
quantity
}) => acc.set(itemName, (acc.get(itemName) || 0) + quantity));
return acc;
}, new Map), ([itemName, quantity]) => ({
itemName,
quantity
}));
result.sort((a, b) => {
if (a.quantity > b.quantity) {
return -1;
} else if (a.quantity < b.quantity) {
return 1;
} else {
return 0;
}
});
function displayShelfItemPair(shelfName, itemName) {
let table = document.createElement('table');
document.body.appendChild(table);
for (let r in row) {
let tr = document.createElement('tr');
table.appendChild(tr); // Append to <table> node
for (let c in cell) {
let tdElement = document.createElement('td');
tdElement.innerHTML = result[i][j];
tr.appendChild(tdElement);
}
}
console.log(displayShelfItemPair(shelfName, itemName));
}
}
main();
DESIRED OUTPUT
ShelfName | itemName (w/quantity)
1 Item 12: 68
2 ''
'' ''
'' ''
You were iterating through objects that did not exist. For instance, for (let r in row) there is no row. I rewrote the function to hopefully get you closer.
function main() {
let yesterdaysOrders = [
{
id: 1,
orderLines: [{
itemName: "Item 01",
quantity: 1
},
{
itemName: "Item 02",
quantity: 3
},
{
itemName: "Item 03",
quantity: 25
},
{
itemName: "Item 04",
quantity: 12
},
],
},
{
id: 2,
orderLines: [{
itemName: "Item 01",
quantity: 1
},
{
itemName: "Item 08",
quantity: 42
},
{
itemName: "Item 09",
quantity: 13
},
{
itemName: "Item 12",
quantity: 37
},
],
},
{
id: 3,
orderLines: [{
itemName: "Item 12",
quantity: 16
}, ],
},
{
id: 4,
orderLines: [{
itemName: "Item 10",
quantity: 11
},
{
itemName: "Item 11",
quantity: 10
},
],
},
{
id: 5,
orderLines: [{
itemName: "Item 06",
quantity: 7
},
{
itemName: "Item 07",
quantity: 2
},
{
itemName: "Item 12",
quantity: 14
},
],
},
{
id: 6,
orderLines: [{
itemName: "Item 05",
quantity: 17
}, ],
},
{
id: 7,
orderLines: [{
itemName: "Item 03",
quantity: 5
},
{
itemName: "Item 07",
quantity: 2
},
],
},
{
id: 8,
orderLines: [{
itemName: "Item 02",
quantity: 13
},
{
itemName: "Item 07",
quantity: 7
},
{
itemName: "Item 09",
quantity: 2
},
],
},
{
id: 9,
orderLines: [{
itemName: "Item 01",
quantity: 4
},
{
itemName: "Item 06",
quantity: 17
},
{
itemName: "Item 07",
quantity: 3
},
],
},
{
id: 10,
orderLines: [{
itemName: "Item 11",
quantity: 12
},
{
itemName: "Item 12",
quantity: 1
},
],
}
],
result = Array.from(
yesterdaysOrders.reduce((acc, {
orderLines
}) => {
orderLines.forEach(({
itemName,
quantity
}) => acc.set(itemName, (acc.get(itemName) || 0) + quantity));
return acc;
}, new Map), ([itemName, quantity]) => ({
itemName,
quantity
}));
result.sort((a, b) => {
if (a.quantity > b.quantity) {
return -1;
} else if (a.quantity < b.quantity) {
return 1;
} else {
return 0;
}
});
function displayShelfItemPairs() {
let table = document.createElement('table');
document.body.appendChild(table);
table.innerHTML = "<tr><th>ShelfName</th><th>itemName (w/quantity)</th></tr>"
let shelf = 1;
for (let r of result) {
let tr = document.createElement('tr');
table.appendChild(tr); // Append to <table> node
tr.innerHTML = "<td>" + shelf + "</td><td>" + r.itemName + ": " + r.quantity + "</td>";
shelf++
}
}
displayShelfItemPairs()
}
main();
Edit: As I now figured out, output will be the same regardless whether you append children before or after filling them.
In your function displayShelfItemPair, you are appending the new table element before filling it, which is why is displays nothing on the page.
Fill the table element with rows, etc., and then append it to the document:
function displayShelfItemPair(shelfName, itemName) {
let table = document.createElement('table');
for (let i = 0; i < 3; i++) {
let tr = document.createElement('tr');
for (let j = 0; j < 3; j++) {
let tdElement = document.createElement('td');
tdElement.innerHTML = result[i][j];
tr.appendChild(tdElement);
}
table.appendChild(tr); // Append to <table> node AFTER it is filled
}
document.body.appendChild(table); //append table to body AFTER it is filled
console.log(displayShelfItemPair(shelfName, itemName));
}
I'd like to sort through all the itemNames and add all the appropriate quantities together to be able to determine which items were ordered the most.
Here is the array of objects below. I want to try to filter and map through the array of objects and list out the itemName and appropriate totaled quantity for each one, just not sure how to chain together.
function OrderRepository() {
return orderLines.filter((itemName) => {
orderLines.map((quantity) => {
return
}
}
}
Provided array of objects
OrderRepository.prototype.getYesterdaysOrders = function getYesterdaysOrders() {
var yesterdaysOrders = [
{
id: 1,
orderLines: [
{ itemName: "Item 01", quantity: 1 },
{ itemName: "Item 02", quantity: 3 },
{ itemName: "Item 03", quantity: 25 },
{ itemName: "Item 04", quantity: 12 },
],
},
{
id: 2,
orderLines: [
{ itemName: "Item 01", quantity: 1 },
{ itemName: "Item 08", quantity: 42 },
{ itemName: "Item 09", quantity: 13 },
{ itemName: "Item 12", quantity: 37 },
],
},
{
id: 3,
orderLines: [
{ itemName: "Item 12", quantity: 16 },
],
},
{
id: 4,
orderLines: [
{ itemName: "Item 10", quantity: 11 },
{ itemName: "Item 11", quantity: 10 },
],
},
{
id: 5,
orderLines: [
{ itemName: "Item 06", quantity: 7 },
{ itemName: "Item 07", quantity: 2 },
{ itemName: "Item 12", quantity: 14 },
],
},
{
id: 6,
orderLines: [
{ itemName: "Item 05", quantity: 17 },
],
},
{
id: 7,
orderLines: [
{ itemName: "Item 03", quantity: 5 },
{ itemName: "Item 07", quantity: 2 },
],
},
{
id: 8,
orderLines: [
{ itemName: "Item 02", quantity: 13 },
{ itemName: "Item 07", quantity: 7 },
{ itemName: "Item 09", quantity: 2 },
],
},
{
id: 9,
orderLines: [
{ itemName: "Item 01", quantity: 4 },
{ itemName: "Item 06", quantity: 17 },
{ itemName: "Item 07", quantity: 3 },
],
},
{
id: 10,
orderLines: [
{ itemName: "Item 11", quantity: 12 },
{ itemName: "Item 12", quantity: 1 },
],
},
];
return yesterdaysOrders;
};
Desired result
{ itemName: "Item 01", quantity: 6 }
{ itemName: "Item 02", ...... }
I'd like the itemName in order with the totaled quantity listed for that item. So the total number of times Item 01 was ordered yesterday was 6.
You could count the items with the help of a Map and render the wanted result as an array.
var data = [{ id: 1, orderLines: [{ itemName: "Item 01", quantity: 1 }, { itemName: "Item 02", quantity: 3 }, { itemName: "Item 03", quantity: 25 }, { itemName: "Item 04", quantity: 12 }] }, { id: 2, orderLines: [{ itemName: "Item 01", quantity: 1 }, { itemName: "Item 08", quantity: 42 }, { itemName: "Item 09", quantity: 13 }, { itemName: "Item 12", quantity: 37 }] }, { id: 3, orderLines: [{ itemName: "Item 12", quantity: 16 }] }, { id: 4, orderLines: [{ itemName: "Item 10", quantity: 11 }, { itemName: "Item 11", quantity: 10 }] }, { id: 5, orderLines: [{ itemName: "Item 06", quantity: 7 }, { itemName: "Item 07", quantity: 2 }, { itemName: "Item 12", quantity: 14 }] }, { id: 6, orderLines: [{ itemName: "Item 05", quantity: 17 }] }, { id: 7, orderLines: [{ itemName: "Item 03", quantity: 5 }, { itemName: "Item 07", quantity: 2 }] }, { id: 8, orderLines: [{ itemName: "Item 02", quantity: 13 }, { itemName: "Item 07", quantity: 7 }, { itemName: "Item 09", quantity: 2 }] }, { id: 9, orderLines: [{ itemName: "Item 01", quantity: 4 }, { itemName: "Item 06", quantity: 17 }, { itemName: "Item 07", quantity: 3 }] }, { id: 10, orderLines: [{ itemName: "Item 11", quantity: 12 }, { itemName: "Item 12", quantity: 1 }] }],
result = Array.from(
data.reduce((m, { orderLines }) => {
orderLines.forEach(({ itemName, quantity }) => m.set(itemName, (m.get(itemName) || 0) + quantity));
return m;
}, new Map),
([itemName, quantity]) => ({ itemName, quantity })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is what you are looking for:
var hashmap = {}
for(var each of yesterdaysOrders) {
for(var one of each['orderLines']) {
if (one['itemName'] in hashmap) {
hashmap[one['itemName']] += one['quantity']
} else {
hashmap[one['itemName']] = one['quantity']
}
}
}
You can use JavaScript's reduce() method to do that.
let result = yesterdaysOrders.reduce((arr, currentValue) => {
currentValue.orderLines.forEach(order => {
const index = arr.findIndex(item => item.itemName === order.itemName);
if (index === -1) {
arr.push(order);
} else {
arr[index].quantity += order.quantity;
}
});
return arr;
}, []);
And sort the resultant array using Array.sort().
// Sorting on item name in ascending order.
result.sort((a, b) => {
if (a.itemName < b.itemName) {
return -1;
} else if (a.itemName > b.itemName) {
return 1;
} else {
return 0;
}
});
Demo:
var yesterdaysOrders = [
{
id: 1,
orderLines: [
{ itemName: "Item 01", quantity: 1 },
{ itemName: "Item 02", quantity: 3 },
{ itemName: "Item 03", quantity: 25 },
{ itemName: "Item 04", quantity: 12 },
],
},
{
id: 2,
orderLines: [
{ itemName: "Item 01", quantity: 1 },
{ itemName: "Item 08", quantity: 42 },
{ itemName: "Item 09", quantity: 13 },
{ itemName: "Item 12", quantity: 37 },
],
},
{
id: 3,
orderLines: [
{ itemName: "Item 12", quantity: 16 },
],
},
{
id: 4,
orderLines: [
{ itemName: "Item 10", quantity: 11 },
{ itemName: "Item 11", quantity: 10 },
],
},
{
id: 5,
orderLines: [
{ itemName: "Item 06", quantity: 7 },
{ itemName: "Item 07", quantity: 2 },
{ itemName: "Item 12", quantity: 14 },
],
},
{
id: 6,
orderLines: [
{ itemName: "Item 05", quantity: 17 },
],
},
{
id: 7,
orderLines: [
{ itemName: "Item 03", quantity: 5 },
{ itemName: "Item 07", quantity: 2 },
],
},
{
id: 8,
orderLines: [
{ itemName: "Item 02", quantity: 13 },
{ itemName: "Item 07", quantity: 7 },
{ itemName: "Item 09", quantity: 2 },
],
},
{
id: 9,
orderLines: [
{ itemName: "Item 01", quantity: 4 },
{ itemName: "Item 06", quantity: 17 },
{ itemName: "Item 07", quantity: 3 },
],
},
{
id: 10,
orderLines: [
{ itemName: "Item 11", quantity: 12 },
{ itemName: "Item 12", quantity: 1 },
],
},
];
let result = yesterdaysOrders.reduce((arr, currentValue) => {
currentValue.orderLines.forEach(order => {
const index = arr.findIndex(item => item.itemName === order.itemName);
if (index === -1) {
arr.push(order);
} else {
arr[index].quantity += order.quantity;
}
});
return arr;
}, []);
// Sorting on item name in ascending order.
result.sort((a, b) => {
if (a.itemName < b.itemName) {
return -1;
} else if (a.itemName > b.itemName) {
return 1;
} else {
return 0;
}
});
console.log(result);
Please help me, I am stuck with aggregates for kendoTreeList -
I create kendoTreeList, but failed to calculate sum for groups.
$(document).ready(function () {
var _jsondata = [
{ ID: 1, Name: "Parent 1", Amount: "200", parentId: null },
{ ID: 2, Name: "Parent 2", Amount: "500", parentId: null },
{ ID: 11, Name: "Child 11", Amount: "50", parentId: 1 },
{ ID: 12, Name: "Child 12", Amount: "150", parentId: 1 },
{ ID: 21, Name: "Child 21", Amount: "100", parentId: 2 },
{ ID: 22, Name: "Child 22", Amount: "200", parentId: 2 },
{ ID: 23, Name: "Child 23", Amount: "200", parentId: 2 }
];
var dataSource = new kendo.data.TreeListDataSource({
data : _jsondata,
schema : { model: { id: "ID", expanded: true }},
aggregate : [
{ field: "Amount", aggregate: "sum"}
]
});
$("#treelist").kendoTreeList({
dataSource : dataSource,
columns : [
{ field: "Name" , nullable: false },
{ field: "Amount", footerTemplate: "#= sum #"}
]
});
});
this give result as -
Since Amount represented in _jsondata as string, you need to define it in schema.model as number
schema: {
model: {
id: "ID",
expanded: true,
fields: {
Amount: { type: 'number' }
}
}
},
i have the following JavaScript
{
"business": [
{
"order_contents": [
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 85,
"name": "product 3",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 84,
"name": "product 2",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 84,
"name": "product 2",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
}
]
}
]
}
What i am trying to accomplish is when the order comes through a function scans the JSON and creates an array with each unique product name and adds 1 to the quantity each time.
i have tried using a for loop but it loops it the amount of times but doesn't find the name and value in the nested object of each one, it comes back as name = 0 and the value being the individual nested object inside the main object.
A function like the beneath would work. Basically you pass the array as parameter and return an object that 1) gets a new property if the property does not exist yet (eg. the product id), and 2) adds to the count of items when the property does exist. The function below generates an output like: {'product 1': 10, 'product 2': 1, 'product 3': 2}
function getItems(input) {
var arr = input, obj = {};
for (var i = 0; i < arr.length; i++) {
if (!obj[arr[i].name]) {
obj[arr[i].name] = 1;
} else if (obj[arr[i].name]) {
obj[arr[i].name] += 1;
}
}
return obj;
}
// example use
console.log(getItems(order_contents)); // outputs entire object
console.log(getItems(order_contents)['product 1']); // outputs 10
Seeing that you need unique names for each product... you can push the objects into a grouped array of objects then reduce the objects into single unique objects.
var data={"business":[{"order_contents":[{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":85,"name":"product 3","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":84,"name":"product 2","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":83,"name":"product 1","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]},{"id":84,"name":"product 2","price":"1.99","quantity":1,"total":"1.99","ingredients":[],"extras":[]}]}]};
function buildData() {
var items = data.business[0].order_contents, elems = [], groups = [];
for( var i = 0; i < items.length; i++ ) {
Array.prototype.push.call( elems, items[i] );
}
groups.push( groupBy( elems, function( item ) {
return item;
} ) );
groupBy( groups, function( array ) {
for( var i = 0; i < array.length; i++ ) {
var obj = array[i].slice();
Object.keys( obj ).map( function( p ) {
var length = obj.length;
if( obj[p].hasOwnProperty( "quantity" ) ) {
obj[p].quantity = length;
}
groups[i] = obj[p];
} );
}
} );
function groupBy( array, f ) {
var groups = {};
array.forEach( function( o ) {
var group = JSON.stringify( f( o ) );
groups[group] = groups[group] || [];
groups[group].push( o );
} );
return Object.keys( groups ).map( function( group ) {
return groups[group];
} );
}
return groups;
}
(function() {
var old = console.log;
var logger = document.getElementById( 'log' );
console.log = function( message ) {
if( typeof message == 'object' ) {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify( message, undefined, 2 ) : message) + '<br />';
} else {
logger.innerHTML += message + '<br />';
}
}
})();
console.log( buildData() );
<pre id="log">
</pre>
Not a big fan of reinventing the wheel, so here is how you could use object-scan to answer your question
// const objectScan = require('object-scan');
const counts = (haystack) => objectScan(['business[*].order_contents[*].name'], {
filterFn: ({ value, context }) => {
context[value] = (context[value] || 0) + 1;
}
})(haystack, {});
const data = { business: [{ order_contents: [{ id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 85, name: 'product 3', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 84, name: 'product 2', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 83, name: 'product 1', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }, { id: 84, name: 'product 2', price: '1.99', quantity: 1, total: '1.99', ingredients: [], extras: [] }] }]};
console.log(counts(data));
// => { 'product 2': 2, 'product 1': 10, 'product 3': 1 }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#13.7.1"></script>
Disclaimer: I'm the author of object-scan