ReactSensei

Delete One item from array

January 22, 2020

const todos = [
  { text: 'Lift weights', completed: true },
  { text: 'Pay Rent', completed: false },
  { text: 'Do Laundry', completed: true },
  { text: 'Code JavaScript', completed: false },
  { text: 'Run', completed: true },
];

const deleteTodo = function(todosArr, todoText) {
  const index = todosArr.findIndex(function(todo) {
    return todo.text.toLowerCase() === todoText.toLowerCase();
  });
  if (index > -1) {
    todosArr.splice(index, 1);
  }
};

deleteTodo(todos, 'Paya Rent');

console.log(todos);

© 2020