Created
December 7, 2018 01:17
-
-
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
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
/* | |
* 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