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

94
Vistas
Finding same array element in a different array and renaming it to be incremental

Finding same array element in another array and renaming it to be incremental. in the script below , i would like to increment the same element to +1 dynamically without manually putting in "motor" for it to be rename as "motor 1"

For an example , use case

//original array
["car","motor","bicycle","tricyle","motor"]

//result should be 
["car","motor","bicycle","tricyle","motor 1"]

This is the script i tried

let _temparr = ["car","motor","bicycle","tricyle","motor"]
let _newarr  = new Array();
let counter = 0;

for(let i = 0 ; i < _temparr.length;i++){
  
  // Push _temparr element to _newarr
  for(let a = 0; a < _newarr.length; a++){

    // if _newarr consist of motor , increment counter +1 and push as "Motor 1"
    if(_newarr[i] === "motor"){
      counter++
      _newarr.push(_temparr[i] + counter)
    }
  }
  _newarr.push(_temparr[i])

The logic i came out with is, i will loop _temparray and push it to a new arrau which is :_newarr , within the first loop of _newarr if it finds the same element in _temparray. counter will increment . but im nnot sure what is the best practice to do this. i found some topics on using const found = arr1.some(r=> arr2.includes(r)) but this will remove if it has the same element

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can keep a counter for every word/element and use .map to create the new array, updating each element as needed:

const counter = new Map();
console.log(
  ["car","motor","bicycle","tricyle","motor"].map(
    element => {
      if (!counter.has(element)) {
        counter.set(element, 1);
        return element;
      }
      const count = counter.get(element);
      counter.set(element, count+1);
      return element + " " + count;
    }
  )
);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe you could consider using a Map:

const originalArray = ["car", "motor", "bicycle", "tricyle", "motor"];
const newArray = [];
const counter = new Map();
originalArray.forEach(item => {
  if (counter.has(item)) {
    newArray.push(`${item} ${counter.get(item)}`);
    counter.set(item, counter.get(item) + 1);
  } else {
    newArray.push(item);
    counter.set(item, 1);
  }
});
console.log(newArray);

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