Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, you can use a loop or a function to generate the desired object with multiple IDs instead of manually replacing them. For example:

// Using a loop
const obj = {};
for (let i = 1; i <= 3; i++) {
  obj[`id${i}`] = i;
}

// Using a function
function generateObjectWithIds(count) {
  const obj = {};
  for (let i = 1; i <= count; i++) {
    obj[`id${i}`] = i;
  }
  return obj;
}

const objWithIds = generateObjectWithIds(3); // generates { id1: 1, id2: 2, id3: 3 }