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

327
Views
How to delete an item by index from a state array in React

I want to delete an object from an array of objects into another array in React, but I am a bit confused, so far I can push an object into the array. But abit confused how to delete it by index.

data

const addValues = [
  {
    "name": "New 1",
    "Value": [98,15,7,8]
  }, 
  {
    "name": "New 2",
    "Value": [7,18,9,40]
  },
  {
    "name": "New 3",
    "Value": [0,19,50,98]
  },
  {
    "name": "New 4",
    "Value": [56,89,34,20]
  },
]

const [value, setValue] = useState([]);

const handlePush = () => {
  setValue(oldArray => [...oldArray, newValue]);
};

 <div>{addValues?.map((inside, index) => {
    return (
      <React.Fragment key={index}>
         <p>{inside.name} <button onClick={() => setTags(oldArray => [...oldArray, addValue[index]])}>Add</button></p>
       </React.Fragment>
    )
  })}</div>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you want a subset of an immutable array, that's called a filter operation. And to remove an item by index, then you simply want filtering logic to filter out one specific index.

For example:

const handleRemove = (removeIndex) => {
  setValue(oldArray => {
    return oldArray.filter((value, i) => i !== removeIndex)
  })
}
about 4 years ago · Juan Pablo Isaza Report

0

Can use filter to generated a new array without position you wanted to cut

Like this:

const filteredArray = items.filter((item, index) => index !== value);
about 4 years ago · Juan Pablo Isaza Report

0

You can use the splice function to to this, given an array index:

const handleDelete = (arrayIndex) => {
  setValue(oldArray => oldArray.splice(arrayIndex,1));
};

Inside code

const addValues = [
  {
    "name": "New 1",
    "Value": [98,15,7,8]
  }, 
  {
    "name": "New 2",
    "Value": [7,18,9,40]
  },
  {
    "name": "New 3",
    "Value": [0,19,50,98]
  },
  {
    "name": "New 4",
    "Value": [56,89,34,20]
  },
]

const [value, setValue] = useState([]);

const handlePush = () => {
  setValue(oldArray => [...oldArray, newValue]);
};

const handleDelete = (arrayIndex) => {
  setValue(oldArray => oldArray.splice(arrayIndex,1));
};

 <div>{addValues?.map((inside, index) => {
    return (
      <React.Fragment key={index}>
         <p>{inside.name} <button onClick={() => setTags(oldArray => [...oldArray, addValue[index]])}>Add</button></p>
       </React.Fragment>
    )
  })}</div>


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!