Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active March 31, 2020 21:51
Show Gist options
  • Save sandrabosk/237541ecf75bf2d810ab0ffce7d61e27 to your computer and use it in GitHub Desktop.
Save sandrabosk/237541ecf75bf2d810ab0ffce7d61e27 to your computer and use it in GitHub Desktop.
// Example 1:
const productsAsStrings = [
'iphone', 'mug', 'hat'
]
const deleteProd = toBeDeleted => {
const filteredArr = productsAsStrings.filter(el => el !== toBeDeleted)
return filteredArr;
}
deleteProd('hat')
// return: [ 'iphone', 'mug' ]
// Example 2:
const productsAsObjects = [
{
_id: '34rg',
name: 'iPhone X',
price: 799.99,
inStock: true
},
{
_id: '36gu',
name: 'iron',
price: 29.99,
inStock: false
},
{
_id: '97ux',
name: 'coffee mug',
price: 9.0,
inStock: true
}
]
const deleteProd = toBeDeleted => {
const filteredArr = productsAsObjects.filter(el => el._id !== toBeDeleted._id)
return filteredArr;
}
deleteProd({
_id: '97ux',
name: 'coffee mug',
price: 9.0,
inStock: true
})
// return:
// [
// { _id: '34rg', name: 'iPhone X', price: 799.99, inStock: true },
// { _id: '36gu', name: 'iron', price: 29.99, inStock: false }
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment