Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Question about preferred code conversion
// 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