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

136
Vistas
Cómo cambiar todos los elementos excepto el primero en la matriz para presentar solo la diferencia

Tengo varias matrices que se ven más o menos así:

 let r = [ 'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 20% for 12s.', 'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 25% for 12s.', 'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 30% for 12s.', 'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 35% for 12s.', 'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 40% for 12s.' ]
 let r1 = [ 'Increases Movement SPD by 10%. When in battle, gain an 8% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.', 'Increases Movement SPD by 10%. When in battle, earn an 8% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.', 'Increases Movement SPD by 10%. When in battle, earn a 10% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.', 'Increases Movement SPD by 10%. When in battle, earn a 12% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.', 'Increases Movement SPD by 10%. When in battle, earn a 14% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.' ]

El problema es que quiero que otros elementos en la matriz (sin incluir el primero) solo incluyan la palabra diferente en comparación con los demás.

 // <number>% //

En claro, quiero que todas las líneas siguientes tengan todo reemplazado con // excepto las palabras que cambian. Al final me gustaría un resultado así:

 [ 'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 20% for 12s.', '// 20% //', '// 30% //', '// 35% //', '// 40% //' ]

No tengo absolutamente ninguna idea sobre cómo lograr esto y me gustaría ayuda para crear una función, de modo que cuando proporcione una matriz como esa como entrada, se devuelva el resultado anterior.

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

0

Puedes probar la siguiente función:

 function replaceStatic(array, replacement) { let currSentence = array[0]; // First comparison sentence let splittedCurrSentence = currSentence.split(" "); // Get currSentence words in an array (for further comparison) const newArray = [currSentence]; // The array to return. First sentence can already be in there for (let s = 1, len = array.length; s < len; s++) { // Loop from second to last sentence const sentence = array[s]; // Current sentence to analyse const splittedSentence = sentence.split(" "); // Words array for the sentence to analyze let replace = true; // Set a boolean to handle when you need to replace the current word const mappedSplittedSentence = splittedSentence.map(word => { // Maps every words with itself (if new), the replacement string (if not new a last word was new) or doesn't map if (!splittedCurrSentence.includes(word)) { replace = true; return word; } else if (replace) { replace = false; // Set to false so if next word is also new, there won't be consecutive replacements return replacement; } }).filter(word => word !== undefined); // Remove unmapped words (consecutive already existing words) newArray.push(mappedSplittedSentence.join(" ")); // Stringify the mapped sentence // Set comparison sentence to the current sentence for next iteration // This is done so every check is made compared to previous sentence // You can comment/remove this if you only want to compare to the first sentence splittedCurrSentence = splittedSentence; } return newArray; }

Si llama a su matriz r, devuelve:

 console.log( replaceStatic(r, "//") ); /* Output: [ 'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 20% for 12s.', '// 25% //', '// 30% //', '// 35% //', '// 40% //' ] */

Con r1 da:

 console.log( replaceStatic(r1, "//") ); /* Output: [ 'Increases Movement SPD by 10%. When in battle, gain an 8% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.', '// earn //', '// a 10% //', '// 12% //', '// 14% //' ] */

Probablemente haya mejores soluciones, pero esto debería ser lo suficientemente general como para adaptarse a su caso.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Comenzaría con una "cadena de plantilla" (solo un nombre, no literales de plantilla relacionados) que tiene una frase específica para reemplazar y una matriz de valores para reemplazarla, luego mapear los valores de reemplazo con la cadena de plantilla.

 const templateString = "Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by **percent**% for 12s."; const percentages = [20,25,30,40]; const replacedStrings = percentages.map( function(p) { return templateString.replace("**percent**",p); } ); console.log("replacedStrings",replacedStrings);

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