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

168
Views
how to add object to specific index array in javascript?

I have example data like this

let datas = [
  { no : '1', list: [ {val: '1'}, {val: '3'},{val: '4'}] },
  { no : '2', list: [ {valNew: '11'}, {valNew: '13'},{valNew: '14'}] }
]

his expectations are like this

let datas = [
  { no : '1', list: [ {val: '1'}, { val: '2'}, { val: '3'},{val: '4'}] },
  { no : '2', list: [ {valNew: '11'}, {valNew: '13'},{valNew: '14'}] }
]

I've tried like this

const handleDuplicate = (index, idx, item) => {
let data = [...datas.listInvoice]
      data[index] = {
        ...data[index] = {
          ...data[index],
          invoices: [...data[index].invoices.slice(idx+1, 0, item)]
        }
      }
     
      setDatas(prevState => {
        return {
          ...prevState,
          listInvoice: data
        }
      })
}

I want to add an object to an array with a certain position, but it always fails and the array inside invoices becomes [], what is the solution for this case?

about 4 years ago · Juan Pablo Isaza
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!