I'm building my first framework. I need to be able to generate a semi-dynamic object, to which end, I want to re-use a single variable that points to a unique-value-returning function. I have a JSON formatted string like this:
let myObject = {
[test] : "placeholder text 1",
[test] : "lorem ipsum 2"
};
The [test] bit refers to a function of this sort:
const test = (function() {
return String("morePlaceholderText" + getRandomInt() )
})();
function getRandomInt() {
return Math.floor(Math.random() * 15);
};
...And all that works as it should. It's reporting unique values, so the actual value of the [test] property is a unique string for every property. However, under the hood it seems to be getting treated like a plain string.
If I console.log it, I get only the last occurrence:
console.log(JSON.stringify(myObject));
{"morePlaceholderText7":"lorem ipsum 2"}
So far, I've tried several different structures. It didn't seem to like arrow functions. Doesn't work at all without the square brackets on the property name function.
I could get a similar effect by simply storing a specific property inside my object, with unique object identifier metadata stored as to the value, and I did try that... But the readability impact was so severe on some of the more complex data structures, that got scrapped out as an absolute last ditch resort. If I can get something that's pretty close in brevity and readability to what I have here, that would be ideal.
Do I have something wrong with my syntax somewhere? Having a tough time finding documentation on this sort of thing.
Thanks.