Looking to retrieve the latest 8 products based on their date? Take a look at the code snippet below:
const newestProducts= [];
axios.get("http://localhost:3003/products").then(response => {
let products = response.data.sort(function(a, b) {
return new Date(b.date) - new Date(a.date);
});
newstProducts = product.slice(0,7)
});
However, this method might not be efficient when dealing with thousands of products and only needing the newest 8.
One idea could be to add ?_limit=8
to the API call
axios.get("http://localhost:3003/products?_limit=8").then{...}
Yet, this approach also has its limitations as it fetches only the top 8 products from the array.
Is there a way to filter the products before requesting them from the server, or is storing all products in a variable first necessary?
The JSON File
"categories": [
{
"id": 9,
"category": "bathroom",
"date": "2020/8/3",
"name": "ullam basin mixer",
"price": 160,
"img_1": "rim_mixer_01.jpg",
"img_2": "rim_mixer_02.jpg",
"rating": 4.5,
"description": "MARMO is a complete series made of 72 models, with different shapes and sizes for different functions, that keeps uncompromised its elegant beauty. This is a Demo Online Store."
},
...
]