Skip to main content
added 146 characters in body
Source Link
ADyson
  • 63k
  • 17
  • 96
  • 103

The problem you've got is that you only execute the name-creation function once and assign it to the test variable, before you declare myObject. Therefore it then tries to create two properties with the same name within the same object, which of course isn't allowed.

If you write it so that test() is actually a function, and is called separately for each property in the object, then you'll get a different value back each time (barring an extremely unlikely accident where Math.Randomrandom() returns the same value twice in a row).

Demo:

var props = ["x"]
const test = function() {
  return String("morePlaceholderText" + getRandomInt() )
};
function getRandomInt() {
  return Math.floor(Math.random() * 15);
};

let myObject = {
  [test()] : "placeholder text 1",
  [test()] : "lorem ipsum 2"
};

console.log(JSON.stringify(myObject));

Relevant background info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names

The problem you've got is that you only execute the name-creation function once and assign it to the test variable, before you declare myObject. Therefore it then tries to create two properties with the same name within the same object, which of course isn't allowed.

If you write it so that test() is actually a function, and is called separately for each property in the object, then you'll get a different value back each time (barring an extremely unlikely accident where Math.Random returns the same value twice in a row).

Demo:

var props = ["x"]
const test = function() {
  return String("morePlaceholderText" + getRandomInt() )
};
function getRandomInt() {
  return Math.floor(Math.random() * 15);
};

let myObject = {
  [test()] : "placeholder text 1",
  [test()] : "lorem ipsum 2"
};

console.log(JSON.stringify(myObject));

The problem you've got is that you only execute the name-creation function once and assign it to the test variable, before you declare myObject. Therefore it then tries to create two properties with the same name within the same object, which of course isn't allowed.

If you write it so that test is actually a function, and is called separately for each property in the object, then you'll get a different value back each time (barring an extremely unlikely accident where Math.random() returns the same value twice in a row).

Demo:

var props = ["x"]
const test = function() {
  return String("morePlaceholderText" + getRandomInt() )
};
function getRandomInt() {
  return Math.floor(Math.random() * 15);
};

let myObject = {
  [test()] : "placeholder text 1",
  [test()] : "lorem ipsum 2"
};

console.log(JSON.stringify(myObject));

Relevant background info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names

Source Link
ADyson
  • 63k
  • 17
  • 96
  • 103

The problem you've got is that you only execute the name-creation function once and assign it to the test variable, before you declare myObject. Therefore it then tries to create two properties with the same name within the same object, which of course isn't allowed.

If you write it so that test() is actually a function, and is called separately for each property in the object, then you'll get a different value back each time (barring an extremely unlikely accident where Math.Random returns the same value twice in a row).

Demo:

var props = ["x"]
const test = function() {
  return String("morePlaceholderText" + getRandomInt() )
};
function getRandomInt() {
  return Math.floor(Math.random() * 15);
};

let myObject = {
  [test()] : "placeholder text 1",
  [test()] : "lorem ipsum 2"
};

console.log(JSON.stringify(myObject));