s

jquery remove object from array of objects edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 18 September 2020 | 2674

How do i remove the selected country from a list of countries array which has properties Id and Name? . You can remove the object from a list of objects in javascript using the splice function. It takes three parameters the first parameter is the index and the second parameter is the number of items to remove from that index. If you want to remove 1 object give the second parameter as 1.

I have a list of countries like in the list below. I can use the splice function to remove object with provided id

var lstCountries = [{Id:91, Name:'India'}, {Id:1, Name:"United States Of America"}, {Id:7, Name:"Russia"}]
var index = lstCountries.findIndex(x=> x.Id === 7)
lstCountries.splice(index,1)