let library = []; //this stores each object
Each object has a property called' id' where a number is stored, this represents the index of the object in the library array. Basically when an object is deleted from the library I want to get all the objects in the array that come after the deleted object and minus 1 from the id property.
function updateId(index) {
for (let i = index; i <= library.length; i++) {
library[i].id - 1;
}
}
So far I have this function where the argument index is the position in the array where the object was deleted. I am getting a reference error with that last line of code
Instead of i <= library.length try i < library.length.
Currently that line is saying "continue this for loop so long as i is less than or equal to the length" ... but remember, i (ie. indices) are zero-based, while lengths aren't. This means that you'll actually loop past where you want.
Note this is just one of many ways to solve this! You're almost there. There are a few things that your code needs:
function updateId(library, startingAtIndex) {
for (let i = startingAtIndex; i < library.length; i++) {
library[i].id -= 1;
}
return library
}
things = [{name:'a', id:1},{name:'c', id:3},{name:'d', id:4}]
things = updateId(things, 1)
console.log(things)