📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Remove Object from an Array of Objects in JavaScript
// using Object Literals
var user1 = {
id : 1,
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29
}
var user2 = {
id : 2,
firstName : 'John',
lastName : 'Cena',
emailId : 'john@gmail.com',
age : 29
}
// we have an array of objects, we want to remove one object using only the id property
var users = [user1, user2];
console.log('Before removing object from an array -> ' + JSON.stringify(users));
// get index of object with id:37
var removeIndex = users.map(function(item) { return item.id; }).indexOf(37);
// remove object
users.splice(removeIndex, 1);
console.log('After removing object from an array -> ' + JSON.stringify(users));
Before removing object from an array -> [{"id":1,"firstName":"Ramesh","lastName":"Fadatare","emailId":"ramesh@gmail.com","age":29},
{"id":2,"firstName":"John","lastName":"Cena","emailId":"john@gmail.com","age":29}]
After removing object from an array -> [{"id":1,"firstName":"Ramesh","lastName":"Fadatare","emailId":"ramesh@gmail.com","age":29}]
Comments
Post a Comment
Leave Comment