0

Edited to include a reproducible example. When a new task is created, a quantity can be input to create multiple tasks with the same data. In this case, the quantity is 3, so three tasks will be created with the other name:value pairs within the newtaskconfig object. The quantity is used in the for loop to push that many tasks to the newtasks array. An id with a random three digit integer is then assigned to each object within the newtasks array.

newtaskconfig = {
  site: 'YeezySupply',
  findby: 'URL',
  urlorpid: 'test.com',
  useproxies: 'TRUE',
  quantity: 3
}
quantity = Number(newtaskconfig.quantity)
delete newtaskconfig.quantity
newtasks=[]
for (i = 0; i < quantity; i++)  
{ 
  newtasks.push(newtaskconfig)
}
newtasks.forEach(task=>{
  task.id=Math.floor(Math.random()*(900)+100)
})

When I then log the newtasks array to console, instead of each object having a unique id, they all end up with the exact same one, shown here:

[
  {
    site: 'YeezySupply',
    findby: 'URL',
    urlorpid: 'test.com',
    useproxies: 'TRUE',
    id: 346
  },
  {
    site: 'YeezySupply',
    findby: 'URL',
    urlorpid: 'test.com',
    useproxies: 'TRUE',
    id: 346
  },
  {
    site: 'YeezySupply',
    findby: 'URL',
    urlorpid: 'test.com',
    useproxies: 'TRUE',
    id: 346
  }
]

How can I change my approach so that each object within the array is assigned a unique three digit id?

5
  • 3
    It seems like the problem is that your array contains multiple references to the same object. Or you were just very unlucky. Commented May 22, 2020 at 8:02
  • 1
    Your "array of objects" is just an array of three pointers to the same object. Commented May 22, 2020 at 8:02
  • Yes, as these are three unique tasks, which is why I am intending to assign them a unique id. Commented May 22, 2020 at 8:04
  • 1
    I'm not sure what you think you're saying yes to. You might consider them unique tasks, but it seems like they're actually not even separate objects. Give a minimal reproducible example, if you create them with literals as shown, the code you've posted works. Commented May 22, 2020 at 8:07
  • I have tried my best to edit my question to provide this, please let me know if you need more clarity Commented May 22, 2020 at 8:17

1 Answer 1

1

Just change the line to newtasks.push({...newtaskconfig}), instead of newtasks.push(newtaskconfig) which causing have same ref.

Update: as @jonrsharpe suggest, simplify the code to the following.

newtaskconfig = {
  site: "YeezySupply",
  findby: "URL",
  urlorpid: "test.com",
  useproxies: "TRUE",
  quantity: 3
};

const { quantity, ...rest } = newtaskconfig;
const newtasks = new Array(quantity)
  .fill(0)
  .map(() => ({ ...rest, id: Math.floor(Math.random() * 900 + 100) }));

console.log(newtasks);

Sign up to request clarification or add additional context in comments.

2 Comments

You could simplify to newtasks.push({ ...newtaskconfig, id: ... }). Note that this is only a shallow clone.
Thanks @jonrsharpe, Updated answer to further simplify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.