Skip to content Skip to sidebar Skip to footer

Replacing A Duplicate Value In An Array With A Set Value

I'm trying to build an array that will replace duplicate values will a set value. I'm not sure how to go about doing it though. var enemy1 = 400 + $gameTroop.members()[0].e

Solution 1:

You could take a Set and map a different value for seen values.

const 
    data = [410, 410, 412],
    result = data.map((s => v => s.has(v)
        ? 1000
        : (s.add(v), v)
    )(new Set));

console.log(result);

Solution 2:

By reconstructing to a Map method I was able to compare variables to themselves a reset the map keys and values to their appropriate values.

var enemy1 = 400 + $gameTroop.members()[0].enemyId();
        var enemy2 = 400 + $gameTroop.members()[1].enemyId();
        var enemy3 = 400 + $gameTroop.members()[2].enemyId();
        let myMap = new Map();
        
        myMap.set(0, enemy1);
        
        if(enemy2 == enemy1) {
            myMap.set(1, 1000);
        } else {
            myMap.set(1, enemy2);
        }
        
        if(enemy3 == enemy2 || enemy3 == enemy1) {
            myMap.set(2, 1000);
        } else {
            myMap.set(2, enemy3);
        }

Thank you @enhzflep for the suggestion.


Post a Comment for "Replacing A Duplicate Value In An Array With A Set Value"