Last active
October 13, 2022 13:15
-
-
Save trafficinc/f6cfcc190db73a072c8074d361c093fb to your computer and use it in GitHub Desktop.
Delete Object from Array/Collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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