Skip to content

Instantly share code, notes, and snippets.

@trafficinc
Last active October 13, 2022 13:15
Show Gist options
  • Save trafficinc/f6cfcc190db73a072c8074d361c093fb to your computer and use it in GitHub Desktop.
Save trafficinc/f6cfcc190db73a072c8074d361c093fb to your computer and use it in GitHub Desktop.
Delete Object from Array/Collection
let startTime = new Date();
// delete an object in a array/collection
const collection = [
{id:3, name:"Item #1",age:32},
{id:4, name:"Item #2",age:33},
{id:5, name:"Item #3",age:34},
{id:6, name:"Item #4",age:35},
{id:7, name:"Item #5",age:36},
{id:8, name:"Item #6",age:47},
];
function removeObj(collection, id) {
return collection.filter(function (o) {
return o.id !== id;
});
}
console.log(collection);
let newCollection = removeObj(collection, 3);
console.log(newCollection);
console.log(`It took ${new Date() - startTime} milliseconds to calculate.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment