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

93
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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