programming-examples/js/Object/Retrieve all the values of an object's properties.js

23 lines
696 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function all_values(obj) {
var keys = _keys(obj);
var length = keys.length;
var values = Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
}
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(all_values({red: "#FF0000", green: "#00FF00", white: "#FFFFFF"}));