Ask Your Question
2

Is there a way to avoid manually replacing "id: 1" multiple times with "id: 1, id: 2, id: 3" in an object in VS Code?

asked 2021-11-20 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-10-07 05:00:00 +0000

pufferfish gravatar image

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 }
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-20 11:00:00 +0000

Seen: 9 times

Last updated: Oct 07 '22