Skip to content

Instantly share code, notes, and snippets.

@ordoghl
Created April 20, 2018 11:12
Show Gist options
  • Save ordoghl/3001174901107424dbf58711aa5dd00b to your computer and use it in GitHub Desktop.
Save ordoghl/3001174901107424dbf58711aa5dd00b to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/puhefis
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
"use strict";
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var fieldSections = { "a": 1, "b": 1, "c": 2, "d": 2, "e": 3, "f": 1, "g": 1 };
var displayModes = { "a": "rejected", "d": "corrected", "e": "rejected", "f": "rejected", "g": "corrected" };
var result = Object.keys(displayModes).reduce(function (acc, field) {
return _extends({}, acc, _defineProperty({}, fieldSections[field], _extends({}, acc[fieldSections[field]] || [], _defineProperty({}, displayModes[field], [].concat(_toConsumableArray(acc[fieldSections[field]] && acc[fieldSections[field]][displayModes[field]] || []), [field])))));
}, {});
console.log(result);
</script>
<script id="jsbin-source-javascript" type="text/javascript">const fieldSections = { "a": 1, "b": 1, "c": 2, "d": 2, "e": 3, "f": 1, "g": 1 }
const displayModes = { "a": "rejected", "d": "corrected", "e": "rejected", "f": "rejected", "g": "corrected" }
const result = Object.keys(displayModes)
.reduce((acc, field) => ({
...acc,
[fieldSections[field]]: {
...(acc[fieldSections[field]] || []),
[displayModes[field]]: [
...((acc[fieldSections[field]] && acc[fieldSections[field]][displayModes[field]]) || []),
field
]
}
}), {})
console.log(result)</script></body>
</html>
"use strict";
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var fieldSections = { "a": 1, "b": 1, "c": 2, "d": 2, "e": 3, "f": 1, "g": 1 };
var displayModes = { "a": "rejected", "d": "corrected", "e": "rejected", "f": "rejected", "g": "corrected" };
var result = Object.keys(displayModes).reduce(function (acc, field) {
return _extends({}, acc, _defineProperty({}, fieldSections[field], _extends({}, acc[fieldSections[field]] || [], _defineProperty({}, displayModes[field], [].concat(_toConsumableArray(acc[fieldSections[field]] && acc[fieldSections[field]][displayModes[field]] || []), [field])))));
}, {});
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment