Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
How to change all the elements but the first one in the array to only present the difference

I have multiple arrays that look more or less like so :

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.'
]

The problem is that I want other elements in the array (not including the first one) to only include the word different compared to the others.

// <number>% //

In clear, I want that all the next lines will have everything replaced with // except the words that change. In the end I would like a result like so :


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

I have absolutely no idea on how to accomplish this and I would like help on creating a function so when I give an array like that as input, the result above get returned.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can try the following function:

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

If you call your r array it returns:

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% //'
]
*/

With r1 it gives:

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% //'
]
*/

There are probably better solutions but this should be general enough to fit your case.

about 4 years ago · Juan Pablo Isaza Report

0

I would start with a "template string" (just a name, not realted template literals) that has a specific phrase to be replaced, and an array of the values to replace it with, then map the replacement values with the template string.

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!