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

164
Views
How to remove Item from an array with react and firebase

When I add an item to the array it works but the splice does not.

const handleSportsFollowed = async (sport) => {
  if (selectedSports.includes(sport)) {
    selectedSports.splice(sport, 1);
    alert("Removed");
  } else {
    selectedSports.push(sport);
  }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can remove elements that are equal to sport from the array using:

selectedSports = selectedSports.filter(x => x != sport)
about 4 years ago · Juan Pablo Isaza Report

0

const a = ['sport', 'sport1', 'sport2', 'sport3', 'sport3'];
const b = a.filter(x => x != 'sport');
console.log(b);

about 4 years ago · Juan Pablo Isaza Report

0

You need to find the index for splice. Check Document how splice work

const handleSportsFollowed = async (sport) => {
 if (selectedSports.includes(sport)) {
   selectedSports.splice(selectedSports.indexOf(sport), 1);
    console.log("if selectedSports ", selectedSports);
  } else {
     selectedSports.push(sport);
    console.log("else selectedSports", selectedSports);
  }

Working example

const selectedSports = ['Jan', 'March', 'April', 'June'];

const testFun = (sport) => {
if (selectedSports.includes(sport)) {
  selectedSports.splice(selectedSports.indexOf(sport), 1);
  console.log("selectedSports ", selectedSports);
} else {
  selectedSports.push(sport);
  console.log("selectedSports", selectedSports);
}
}

testFun("March");

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!