I am iterating through data and doing want to assign a random number to a fontObj only if it is unique
a fontObj looks like:
{
postscript: "Calibri",
style: "Bold",
family: "Calibri"
}
I want to iterate through paragraphs in my code and use the fontObj as a key.
pseudocode:
if (fontMap[fontObj]) {
console.log("Already found: " + fontObj + " and the random number is " + fontMap[fontObj])
} else {
fontMap[fontObj] = Math.random()
}
what is the best way to structure that since I can't check the existence of an entire object using a key?
You can't use an object as a key, you have to use a string.
If the objects all have the same properties, simply make a string out of those values.
function fontObjToKey(fontObj) {
return `${fontObj.postscript}/${fontObj.style}/${fontObj.family}`;
}
let fontKey = fontObjToKey(fontObj);
if (fontMap[fontKey]) {
console.log("Already found: " + fontKey + " and the random number is " + fontMap[fontKey]);
} else {
fontMap[fontKey] = Math.random();
}