programming-examples/js/Object/Create a copy of the object where the keys have become the values and the values the keys .js

22 lines
685 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function invert_key_value(obj) {
var result = {};
var keys = _keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result;
}
function _keys(obj)
{
if (!isObject(obj)) return [];
if (Object.keys) return Object.keys(obj);
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
return keys;
}
function isObject(obj)
{
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
console.log(invert_key_value({red: "#FF0000", green: "#00FF00", white: "#FFFFFF"}));