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

197
Visualizações
How to replace matched values based on ther index .replace() javascript?

I have an object

{
  key: "#% chance to gain Unholy Might for # seconds on Kill", 
  value: [
    [10, 15],
    [3, 3]
  ]
}

I need to replace key "#" in order:

  • first "#" = value[0]
  • second "#" = value[1]

end key must look (10-15)% chance to gain Unholy Might for 3 seconds on Kill

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

0

You need to iterate over the values using Array.prototype.reduce(). Then for every value update the text using String.prototype.replace():

const data = {
  key: "#% chance to gain Unholy Might for # seconds on Kill", 
  value: [
    [10, 15],
    [3, 3]
  ]
};

const replateBy = (text, token, values) => {
  return values.reduce((text, [a,b])=>  text.replace(token, `${a===b ? a: `(${a}-${b})`}`), text);
}
console.log(replateBy(data.key, "#", data.value));

about 4 years ago · Santiago Trujillo Relatório

0

The simplest way to do this is with a replace. Since the first # is followed by a percentage sign we can just replace #% to start off with, which then leaves us with just the last # to replace.

const data = {
  key: "#% chance to gain Unholy Might for # seconds on Kill", 
  value: [
    [10, 15],
    [3, 3]
  ]
}

// First we replace the #% with the actual percentage
const stringWithPercentage = data.key.replace('#%', `(${data.value[0][0]}-${data.value[0][1]})%`)

// Then we can use that value and replace the last # with the seconds
const stringWithSeconds = stringWithPercentage.replace('#', data.value[1][0])

console.log(stringWithSeconds)

There are several ways of achieving this depending on your needs and your willingness to change the data structure.

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