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

100
Visualizações
equivalent indexOf for find value

im a new blocked because I am learning

i want to remplace a value in array without knowing the position of each word (like tab[0]).

The problem is that you have to find a word, followed by another (search test and remplace test-blabla by another like newblabla

I thought about

array = ['test blabla', 'haha hihi']
let index = array.indexOf("test");
if (index !== -1) {
        array[index] = newblabla;
      }
      console.log(index)

it return -1 because it's not 'test' its 'test blabla' but I would like that to be replaced 'test blabla' by 'newblabla'

how can i do that ?

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are trying to find indexOf variable test, which is undefined.

let list = ['test blabla', 'haha hihi']

// here we are doing following things
// 1. We are mapping trough each item of an array
// 2. If item of an array is equals to 'test blabla' then replace it with 'newblabla'
// 3. If item is not equal to 'test blabla', return it without modifing
list = list.map(item => item === 'test blabla' ? 'newblabla' : item);

console.log(list);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a simple map.

const arr = ['a', 'b', 'c']

// Replace 'b' by something else

const newArr = arr.map(v => (v === 'b' ? 'something else' : v))

console.log(newArr)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can easily achive the result using map and replace

const array = ["test blabla", "haha hihi"];

function searchAndReplace(arr, text) {
  return arr.map((str) => {
    const splitArr = str.split(" ");
    return splitArr[0] === text ? splitArr[1] : str;
  });
}
console.log(searchAndReplace(array, "test"));

about 4 years ago · Juan Pablo Isaza 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