简体   繁体   English

如何将随机 integer 分配给数组 JavaScript 中的每个 object

[英]How to assign a random integer to every object in an array JavaScript

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.在本例中, quantity为 3,因此将使用newtaskconfig object 中的其他name:value对创建三个任务。 The quantity is used in the for loop to push that many tasks to the newtasks array.该数量在 for 循环中用于将这么多任务推送到newtasks数组。 An id with a random three digit integer is then assigned to each object within the newtasks array.然后将具有随机三位数 integer 的 id 分配给newtasks数组中的每个 object。

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:然后,当我将 newtasks 数组记录到控制台时,不是每个 object 都有唯一的 id,它们最终都得到完全相同的 ID,如下所示:

[
  {
    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?如何更改我的方法,以便为数组中的每个 object 分配一个唯一的三位数 ID?

Just change the line to newtasks.push({...newtaskconfig}) , instead of newtasks.push(newtaskconfig) which causing have same ref.只需将行更改为newtasks.push({...newtaskconfig}) ,而不是导致具有相同引用的newtasks.push(newtaskconfig)

Update: as @jonrsharpe suggest, simplify the code to the following.更新:正如@jonrsharpe 建议的那样,将代码简化为以下内容。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中为数组中的每个对象分配一个随机数? - How can I assign a random number to each object in an array in javascript? 如何将数组转换为对象并在Javascript中赋值? - How to convert array to object and assign value in Javascript? 如何从 Javascript 中的数组中分配 object 值 - How to assign object values from an array in Javascript 如何在JavaScript中生成随机数并将其分配给我的变量数组? - How to generate a random number and assign it to my variable array in JavaScript? 如何从JavaScript中的对象中删除“整数”数组 - How to remove “integer” array from object in JavaScript 如何在 object 数组 Javascript 中添加随机值? - How to add random values in an object array Javascript? Javascript - 如何将数组中对象的第一个元素分配为另一个数组的键 - Javascript - How to assign the first element of an object in array to be the key of another array 如何为 javascript 中该范围内的每个数字在某个范围内获得唯一的随机 integer? - How to get a unique random integer in a certain range for every number in that range in javascript? 将随机整数分配给数组中的项目,而summary应该是固定数字 - Assign random integer to items in array and summa should ever be a fixed number 在JavaScript中将对象值分配为数组 - Assign object value as array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM