如何制作包含一些克隆值和每个变量的唯一值的变量的唯一克隆
How to make a unique clone of a variable containing some clonded vlaues and unique value per variable
let car = {
brand: 'Ford',
model: '556',
licensePlate: 55554,
}
console.log(car);
for (let i = 1; i < 6; i++) {
let car1 = Object.assign({}, car);
car1.licensePlate += i
console.log(car1);
};
这会创建一个唯一的 licensePlate,但我怎样才能让它创建 car1、car2、car3、car4、car5?
car1 = {
brand: 'Ford',
model: '556',
licensePlate: 55554,
}
car2 = {
brand: 'Ford',
model: '556',
licensePlate: 55555,
}
car3 = {
brand: 'Ford',
model: '556',
licensePlate: 55556,
}
等...
我尽量不使用它,因为它是多余的
let car1 = [...car, LicencePlate: +1]; let car2= [...car, LicencePlate: +2];
在 JavaScript 还是新的,感谢任何帮助。简而言之,是否可以创建一个 for 循环来创建唯一变量,它是如何工作的?
如果你需要car1,car2等键,你应该使用对象,否则数组...
let base = {
brand: 'Ford',
model: '556',
licensePlate: 55554,
}
let carsObj = {};
let carsArr = [];
for (let i = 1; i < 6; i++) {
carsObj[`car${i}`] = {
brand: base.brand,
model: base.model,
licensePlate: base.licensePlate + i
}
carsArr.push({
brand: base.brand,
model: base.model,
licensePlate: base.licensePlate + i
});
};
let car = {
brand: 'Ford',
model: '556',
licensePlate: 55554,
}
console.log(car);
for (let i = 1; i < 6; i++) {
let car1 = Object.assign({}, car);
car1.licensePlate += i
console.log(car1);
};
这会创建一个唯一的 licensePlate,但我怎样才能让它创建 car1、car2、car3、car4、car5?
car1 = {
brand: 'Ford',
model: '556',
licensePlate: 55554,
}
car2 = {
brand: 'Ford',
model: '556',
licensePlate: 55555,
}
car3 = {
brand: 'Ford',
model: '556',
licensePlate: 55556,
}
等...
我尽量不使用它,因为它是多余的
let car1 = [...car, LicencePlate: +1]; let car2= [...car, LicencePlate: +2];
在 JavaScript 还是新的,感谢任何帮助。简而言之,是否可以创建一个 for 循环来创建唯一变量,它是如何工作的?
如果你需要car1,car2等键,你应该使用对象,否则数组...
let base = {
brand: 'Ford',
model: '556',
licensePlate: 55554,
}
let carsObj = {};
let carsArr = [];
for (let i = 1; i < 6; i++) {
carsObj[`car${i}`] = {
brand: base.brand,
model: base.model,
licensePlate: base.licensePlate + i
}
carsArr.push({
brand: base.brand,
model: base.model,
licensePlate: base.licensePlate + i
});
};