Last active
June 23, 2018 23:26
-
-
Save vicsstar/ca05bec37b16ec4eff550c3d0abac68c to your computer and use it in GitHub Desktop.
Tiny script that suffixes an underscore and number to a list of numbers, based on their index in their own group of equal numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Replace the contents of the "string" variable with the actual input numbers you have. | |
* Make sure you space them, with each number on its own line. | |
*/ | |
const numbers = ` | |
2 | |
1 | |
2 | |
3 | |
1 | |
5 | |
3 | |
1 | |
4 | |
3 | |
4 | |
`; | |
numbers.split('\n').filter(a => a.trim() !== '').reduce((acc, num) => { | |
if (acc.length === 0) { | |
acc.push([num]); | |
} else { | |
const arr = acc.find(n => n.includes(num)); | |
if (arr) arr.push(num); | |
else acc.push([num]); | |
} | |
return acc; | |
}, []) | |
// to sort the numbers first, just uncomment the next line by removing the two "//" at the beginning of the line. | |
// .sort((arr1, arr2) => arr1[0] > arr2[0]) | |
.forEach(array => { | |
array.forEach((num, index) => { | |
console.log(`${num}_${index + 1}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment