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

102
Views
pushing object to array in reactjs?

i have a problem with pushing object to array.. i want a result like this.. [{b:"a"},{b:"c"}]

with this code

const [list, setList] - useState({occupation_id: ""});

const handleAdd = () => {
    let arr = [];
    const newData = {...list};
    newData[e.target.id] = e.target.value;
    arr.push(newData);
    setList(arr);
}

but the result from the code below is

[{{occupation_id: "2"}, occupation_id: "1"}]

what the problem with my code?

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

0

You can solve it easily with this solution, Try this:

const [list, setList] = useState([]); // your initial data
const handleAdd = () => {
 setList([...list, {e.target.id:e.target.value}]);
}
about 4 years ago · Juan Pablo Isaza Report

0

Try this :

const [list, setList] = useState([]);
const handleAdd = () => {
 setList([...list, {[e.target.id]:e.target.value}]);
}
about 4 years ago · Juan Pablo Isaza Report

0

const [list, setList] = useState([{occupation_id: "1"}, {occupation_id: "2"}]);

  const handleAdd = (id, value) => {
    const newData = [...list]  
    newData.push({id, value});
    setList(newData);
    console.log(newData)
}
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!