Skip to content

Instantly share code, notes, and snippets.

@et4891
Created December 7, 2018 01:17
Show Gist options
  • Save et4891/e8a114a577ba789fcf3f2fda0b4eda0c to your computer and use it in GitHub Desktop.
Save et4891/e8a114a577ba789fcf3f2fda0b4eda0c to your computer and use it in GitHub Desktop.
return only the fields needed in the array of object by stating the fields need in return
/*
* return obj with the field, used in mostly map()
* @param x {Object}
* @param fields {Array}
* @returns {Object[]}
*
* Sample Usage
* const returnFields = ['amount', 'created', 'status'];
* ArrayObjects.map(x => returnObj(x, returnFields));
*
* Sample return
{ amount: x.amount,
created: x.created,
status: x.status }
* */
returnObj: (x, fields) => {
let result = {};
for (let i = 0; i < fields.length; i++){
result[fields[i]] = x[fields[i]];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment