Created
November 15, 2022 15:48
-
-
Save joemaller/c22a2080179fc0d384ee887ee62453f1 to your computer and use it in GitHub Desktop.
Question about preferred code conversion
This file contains 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
// Starting with an API returned data structure like this: | |
const Parameters = [ | |
{ | |
Name: "PASS", | |
Type: "SecureString", | |
Value: "cGFzc3dvcmQ=", | |
Version: 3, | |
LastModifiedDate: "2022-11-14T15:13:08.263Z", | |
ARN: "arn:aws:ssm:us-east-1:678000000000:parameter/PASS", | |
DataType: "text", | |
}, | |
{ | |
Name: "USER", | |
Type: "String", | |
Value: "username", | |
Version: 2, | |
LastModifiedDate: "2022-11-14T15:02:59.449Z", | |
ARN: "arn:aws:ssm:us-east-1:678000000000:parameter/USER", | |
DataType: "text", | |
}, | |
]; | |
// Which of these two conversion options do you prefer? End result is the same | |
// Assume Prettier is enforced, so code-formatting can't be changed | |
// 1. Predefined empty object, forEach loop | |
const paramsForEach = {}; | |
Parameters.forEach((param) => (paramsForEach[param.Name] = param.Value)); | |
// 2. Reduce returns an object | |
const paramsReduce = Parameters.reduce((map, param) => { | |
map[param.Name] = param.Value; | |
return map; | |
}, {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment