Skip to content

Instantly share code, notes, and snippets.

@jurgob
Created February 19, 2016 17:18
Show Gist options
  • Save jurgob/1fc587c45f6edfa6dff1 to your computer and use it in GitHub Desktop.
Save jurgob/1fc587c45f6edfa6dff1 to your computer and use it in GitHub Desktop.
mapFilter array
const mapFilter = (curArray, filterCb, callback) => curArray.map((el, idx) => filterCb(el,idx) ? callback(el,idx) : el )
const testArray = [
{friends:10, status:"happy"},
{friends:0, status:"happy"},
{friends:20, status:"happy"},
{friends:0, status:"happy"},
{friends:30, status:"super happy"}
];
const res = mapFilter(testArray, (el) => (el.friends ===0) , (el) => { return {...el, status:"forever alone!" }} )
console.log('res ',res);
/* res:
[
{friends:10, status:"happy"},
{friends:0, status:"forever alone!"},
{friends:20, status:"happy"},
{friends:0, status:"forever alone!"},
{friends:30, status:"super happy"}
];
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment