-4
const products = [
  {
    id: 1,
    name: "Jr.Henry",
    command: "Seni takip etmeye başladı",
    img: require("../../assets/imgs/batman-icon.png"),
    follow: false,
    time: "23 dakika",
    follower: 0
  },
  {
    id: 2,
    name: "Artsy",
    command: "Oy verdi",
    img: require("../../assets/imgs/batman-icon.png"),
    follow: false,
    time: "23 dakika",
    follower: 0
  },
 ]

This is my array. How can I delete an array item from state? Can anybody help me ? I need help.

ridvanaltun
  • 2,595
  • 2
  • 15
  • 28
doğan kısa
  • 43
  • 1
  • 7
  • 1
    Does this answer your question? [How to delete an item from state array?](https://stackoverflow.com/questions/36326612/how-to-delete-an-item-from-state-array) – EspressoBeans Oct 07 '21 at 16:10
  • im afraid no. my array list is problem. i dont know. i cant add set to this array and cant get index – doğan kısa Oct 07 '21 at 16:12
  • Could do with a bit more information as to exactly what you want to do here. Do you want to delete the array itself from state? Or just remove an object from said array? If the latter then which object would you like to remove? – P. Brew Oct 08 '21 at 10:12

1 Answers1

-1
const products = [
  {
    id: 1,
    name: "Jr.Henry",
    command: "Seni takip etmeye başladı",
    img: require("../../assets/imgs/batman-icon.png"),
    follow: false,
    time: "23 dakika",
    follower: 0,
  },
  {
    id: 2,
    name: "Artsy",
    command: "Oy verdi",
    img: require("../../assets/imgs/batman-icon.png"),
    follow: false,
    time: "23 dakika",
    follower: 0,
  },
];

// Add an element
products.push({
  id: 3,
  name: "Artsy",
  command: "Oy verdi",
  img: require("../../assets/imgs/batman-icon.png"),
  follow: false,
  time: "23 dakika",
  follower: 0,
});

// remove an element at index
delete products[1];

// In case you want to remove en array element in redux
// you must recreate the array without the element you want to remove
function removeItem(array, index) {
  return [...products.slice(0, index), ...products.slice(index + 1)];
}
ridvanaltun
  • 2,595
  • 2
  • 15
  • 28
Michael Bahl
  • 2,941
  • 1
  • 13
  • 16