Original Code
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
});;
};
Logs
{
car1: { brand: 'Ford', model: '556', licensePlate: 55555 },
car2: { brand: 'Ford', model: '556', licensePlate: 55556 },
car3: { brand: 'Ford', model: '556', licensePlate: 55557 },
car4: { brand: 'Ford', model: '556', licensePlate: 55558 },
car5: { brand: 'Ford', model: '556', licensePlate: 55559 }
}
Now How can I remove car2 and car4 from the carsObj, using it's Index location like [0] and [3], instead of delete carsObj.car2
and then store it in newCarsObj so it logs the following:
{
car1: { brand: 'Ford', model: '556', licensePlate: 55555 },
car3: { brand: 'Ford', model: '556', licensePlate: 55557 },
car5: { brand: 'Ford', model: '556', licensePlate: 55559 }
}
not this answer in link - as this is just an object with property+value. and not an object within object Looking for a solution in JavaScript (without jQuery , lodash etc) - thanks in advance.