Public
Edited
Mar 13, 2023
Insert cell
Insert cell
Insert cell
Insert cell
/**
Rename any duplicate values found in an array using the provided postfix (e.g., '_1') recursively (e.g., '_1_1').
@param {Array} inputArray - The array to modify.
@return {Array} - The array with modified elements.
*/
renameDuplicates = function ( { array, postfix="_1", log=true } = {} ) {
let outputArray = _.cloneDeep( array )
const rename = function ( recursedArray ) {
outputArray = _renameDuplicateElement( { array: recursedArray, postfix: postfix } )
const duplicatesStillRemain = !_.isEqual( outputArray, recursedArray )
if( duplicatesStillRemain ){
rename( outputArray )
}
}
rename(outputArray)
return outputArray
}
Insert cell
Insert cell
Insert cell
_renameDuplicateElement = function( { array, postfix= '_1', log=true } = {} ){
const newArray = _.cloneDeep(array)
for( let i = 0; i < newArray.length; i++){

const element = newArray[i]
const indexOfElementFound = array.indexOf(element)
const actualIndex = i

if (indexOfElementFound !== actualIndex) {
const newElement = element + postfix
newArray[actualIndex] = newElement
if(log){
console.log( `Renamed element ${element} at index ${actualIndex} as ${newElement}.` )
}
break
}

}

return newArray
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { test, expect } from "@hastebrot/unit-testing-with-jest-expect"
Insert cell
must = import("https://cdn.skypack.dev/must@0.13.4")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more