You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
srap/src/util/invert-mapping.js

21 lines
521 B
JavaScript

"use strict";
// Inverts an object of arrays, eg. {a: [x, y], b: [x, z]} becomes {x: [a, b], y: [a], z: [b]}
// TODO: See if this can be replaced with an off-the-shelf module with equivalent semantics, something transpose-y maybe?
module.exports = function invertMapping(mapping) {
let newObject = {};
for (let [ key, values ] of Object.entries(mapping)) {
for (let value of values) {
if (newObject[value] == null) {
newObject[value] = [];
}
newObject[value].push(key);
}
}
return newObject;
};