Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda