Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
Get the firsts json items with axios

I'm working on the front-end of an API in react and I'm trying to get the ten first items of a JSON list like: [{"element1":"value", "element2":"value"}, {"element1":"other value", "element2":"other value"}, ...] But I don't know how to do that with Axios :/ I found nothing in the Axios' Docs or on this forum

Thanks for your help :)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What you are talking about is called pagination and this feature should be implemented on the back end. If pagination is not supported by the backend, then you just have to use regular js array methods, for example, slice https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

about 4 years ago · Juan Pablo Isaza Report

0

Ok, so what you are going to want to use is headers with Axios. Like this:

// headers are like variables that tell your backend what it needs to know
const config = {
  headers: {
    numberOfElements: 5
  }
}

axios.get('/apiendpoint', config)
  .then((response) => {
    // what your backend is sending the frontend
    console.log(response);
  })

Then in your backend you can get the data from that header with express and serve your data:

const express = require('express');
const app = express();

// here is your example data
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

app.get('/urlendpoint', (req, res) => {
  // use the req.get method to get the header data
  const numberOfElements = req.get('numberOfElements');
  // now we can use the arrays .slice() method to get the first x elements of an array and send it in the response
  res.send(numbers.slice(0, numberOfElements));
});

you can read more about the .slice method here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!