I ran into this challenge for my course which i cannot crack for the life of me.
You are given a function with a single parameter which is a nested array with objects.
function sortProducts (matrix) `enter code here`
The array (matrix) looks like this:
[
[
{ product: "MacBook", price: 1019, category: 'tech'},
{ product: "Cheerios", price: 5, category: 'food'},
],
[
{ product: "Snickers", price: 1.5 , category: 'food'},
{ product: "Air Pods", price: 129, category: 'tech'},
],
];
Instructions: Inside the matrix array, each nested array holds objects. Each object represents a product.
The function should loop over the matrix and create a new object containing the products sorted by their category. The function should then return back this result object containing sorted products, stored in properties tech and food.
The result object should have the following structure:
{
tech: [ { tech product }, { tech product } ],
food: [ { food product }, { food product } ],
};
So from my understanding the first step would be to loop over the array to create this new object. This objected would have to sort the products by category in the format (tech: 'tech product', food: 'food product'.
I know the answer is going to be so simple but i cant do it for the life of me.