'How to call only a certain number of arrays in an external api with express.js
I am currently learning about external API's using axios, express, and node. This is the code I am using to call the API:
app.get("/externalapi", (req, res) => {
;
let apiURL = 'https://makeup-api.herokuapp.com/api/v1/products.json';
axios.get(apiURL)
.then(response => {
res.status(200).json(response.data);
})
.catch((err) => {
res.status(500).json({ message: err });
});
});
This code would successfully return ALL the data in that API when I use a GET request from Postman. However, I would only like to call the first 10 arrays of that API. an example data of the API is:
[
{
"id": 495,
"brand": "maybelline",
"name": "Maybelline Face Studio Master Hi-Light Light Booster Bronzer",
"price": "14.99",
"price_sign": null,
"currency": null,
"image_link": "https://d3t32hsnjxo7q6.cloudfront.net/i/991799d3e70b8856686979f8ff6dcfe0_ra,w158,h184_pa,w158,h184.png",
"product_link": "https://well.ca/products/maybelline-face-studio-master_88837.html",
"website_link": "https://well.ca",
"description": "Maybelline Face Studio Master Hi-Light Light Boosting bronzer formula has an expert \nbalance of shade + shimmer illuminator for natural glow. Skin goes \nsoft-lit with zero glitz.\n\n\t\tFor Best Results: Brush over all shades in palette and gently sweep over \ncheekbones, brow bones, and temples, or anywhere light naturally touches\n the face.\n\n\t\t\n\t\n\n ",
"rating": 5,
"category": null,
"product_type": "bronzer",
"tag_list": [],
"created_at": "2016-10-01T18:36:15.012Z",
"updated_at": "2017-12-23T21:08:50.624Z",
"product_api_url": "https://makeup-api.herokuapp.com/api/v1/products/495.json",
"api_featured_image": "//s3.amazonaws.com/donovanbailey/products/api_featured_images/000/000/495/original/open-uri20171223-4-9hrto4?1514063330",
"product_colors": []
}
]
I know you have to use a for loop to call only a certain number of arrays. However, I have almost no idea how to go about that and I would love to learn how to do it. May I please ask of some assistance. I know it's a simple question but I am a beginner trying to learn how to call and manipulate data from external API's
Solution 1:[1]
The .slice() method should help
res.status(200).json(responde.data.slice(0, 10));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Leonardo Bagi |
