I am wondering what is the most efficient way to update an object property that is stored in an array with 10k+ items.
For example if I have an array that holds objects like this {name:””, price:””)
I want to replace or more like update the price values if the array contains that element already.
Check if Array contains object with name = x, if so replace the price with the newest price.
I dont want to have duplicate elements in that array so its not getting to big, i figured I should update it if a property value already exists in it.
So far I have tried several ways like with using indexOf, splice, or just for loops. I am wondering what is the best way performance wise to deal with big arrays.
let array = [
{name:"abc", price: 24},
{name:"cde", price: 25},
{name:"fgh", price: 22},
{name:"gfds", price: 21},
]
function addToArray(elem){
//check if array contains elem by checking for name property value
if(array.filter(el => el.name === elem.name).length > 0){
//update the array element that has the name of the passed elem
}
}