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

90
Views
Fetch data from an API while skipping first object in array

Using react.js, I'm trying to fetch data from an API using the code below:

import React,{useState,useEffect} from 'react';

function App() {
  const [data,setData]=useState([]);
  const getData=()=>{
    fetch('src/data.json')
      .then(function(response){
        return response.json();
      })
      .then(function(myJson) {
        setData(myJson)
      });
  }
  
  useEffect(()=>{
    getData()
  },[])

  return (
    <ul>
      {data.map((item) =>
        <li key={item.id}>{item.name}</li>
      )}
    </ul>
  );
}

export default App;

and api json response is (sample from data.json):

{"users":
  [{"id":1,"name":"John"},{"id":2,"name":"Samantha"}]
}

If I remove the object {"users": } then the code works, but API adds this object to all endpoints.

Is there a way to ignore that first object, or to de-structure it so I get an output of:

  • 1 John
  • 2 Samantha
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If your Javascript interpreter is up to date you can try using the spread operator.

object = {"users":
  [{"id":1,"name":"John"},{"id":2,"name":"Samantha"}]
};
object = {...object.users};

Result:

{ 
  0: {id: 1, name: 'John'},
  1: {id: 2, name: 'Samantha'}
}

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

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!