Last active
March 31, 2020 21:51
-
-
Save sandrabosk/237541ecf75bf2d810ab0ffce7d61e27 to your computer and use it in GitHub Desktop.
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
// 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