Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
Removing a specific string from an array
else if(input === 'remove'){
    let search = prompt('what index do you want to remove');
    for(let j=0;j<todos.length;j++){
        console.log(`current data is:${todos[j]}`);
        if(todos[j] === search){
            console.log(`data to be delete is present`)
            todos.splice(todos[j],1);
            console.log(`data deleted is ${todos[j]}`)
        }
    }

if todos contains [task1, abc, def]

my output is :

current data is:task1

current data is:abc

current data is:def

data to be delete is present

data deleted is undefined

How do I remove the element that is todos[j] from the list todos[]

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can filter() out the unwanted elements

const todos = [ 'bla', 'ble', 'bli', 'foo', 'blo', 'blu', 'bly' ];
const search = 'foo';

const filtered = todos.filter(elem => elem !== search);

console.log(filtered);

Note that the original array will remain unchanged

about 4 years ago · Santiago Trujillo Relatório

0

search = "def";
let todo = ["task1", "abc", "eyg7eg", "ugfuegf7uegf", "def"];
todo.forEach(function (elem, i = 0) {
  if (elem === search) todo.splice(i, 1);
  i++;
});

used for each for iteration the whole array to find the desired element, then if element === search is true then use splice to delete that element

about 4 years ago · Santiago Trujillo Relatório

0

Your code had 2 problems:

  1. It was not deleting the right element (you should do splice(j,1), not splice(todos[j],1)
  2. It was trying to get the value of the deleted element once it had been already deleted.

Fixing theses 2 things, your code works fine, just like this:

let todos = ['task1', 'abc', 'def'];
let search = prompt('what index do you want to remove');

 for(let j=0;j<todos.length;j++){

    let currentData = todos[j];

    console.log(`current data is:${currentData}`);

    if(todos[j] === search){

        console.log(`data to be delete is ${search}`)

        currentData = todos.splice(j,1);

        console.log(`data deleted was ${currentData}`)
    }
}

For instance, CONSOLE EXIT for 'abc' searched:

current data is:task1

current data is:abc

data to be delete is abc

data deleted was abc

HERE I leave you a working copy of your own code with all that fixes.


As an Extra, I give a different version "mixing" several of others mates answers:

const todos = ['task1', 'abc', 'def'];;
const search = 'abc';

todos.some( 
   (item, index) => { if (item === search) { todos.splice(index, 1); return true;} }
);

console.log(todos);

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda