7

This is the function I wrote to retrieve all the values in an given object.

function getValues(data){
    var keys = Object.keys(data);
    var values = [];
    for(var i = 0, l = keys.length, key; i< l; i++){
        key = keys[i];
        values.push(data[key]);
    }
    return values;
}

Is there any builtin way to retrieve all the values in an object? Something like this exists in java for HashMaps. I know JS has a method for retrieving all the keys by doing Object.keys(obj).

6
  • 5
    for..in seems more suitable for objects. Commented Nov 14, 2014 at 1:33
  • if you are using underscore - there is a values function. Commented Nov 14, 2014 at 1:35
  • Not in plain JavaScript, but it's simple to make. Commented Nov 14, 2014 at 1:38
  • 1
    No. There is no built in way to do what you asked. Commented Nov 14, 2014 at 1:42
  • @squint, thx for review, corrected coding mistakes, for values, you are right, but for key, it is defined in for loop... Commented Nov 14, 2014 at 1:46

2 Answers 2

12

Probably the most concise way of getting an array of the values contained within an object is to use Object.keys and Array.prototype.map:

obj = {
    a: 1,
    b: 2,
    c: 3
};
values = Object.keys(obj).map(function (key) {
    return obj[key];
});

Otherwise there's no standardized way of getting an array of an object's values.

For iterating, ES6 introduces a for..of loop which will iterate through an object's values:

continued from above:
for (value of obj) {
    console.log(value); //1, 2, 3
}

ES7 is slated to introduce array comprehensions, so generating the values array could be written as:

continued from above:
values = [for (x of Object.keys(obj)) obj[x]];

If you're already using underscore, you can use the _.values method:

continued from above:
_.values(obj); //[1, 2, 3]

If you just want an efficient implementation for this utility function, the lodash source is:

lodash.js v2.4.1 lines 2891-2914
/**
 * Creates an array composed of the own enumerable property values of `object`.
 *
 * @static
 * @memberOf _
 * @category Objects
 * @param {Object} object The object to inspect.
 * @returns {Array} Returns an array of property values.
 * @example
 *
 * _.values({ 'one': 1, 'two': 2, 'three': 3 });
 * // => [1, 2, 3] (property order is not guaranteed across environments)
 */
function values(object) {
  var index = -1,
      props = keys(object),
      length = props.length,
      result = Array(length);

  while (++index < length) {
    result[index] = object[props[index]];
  }
  return result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Another ES6 possibility: [for (x of Object.keys(obj)) obj[x]]
@squint, wouldn't it just be [for (x of obj) x]?
I don't know if that's planned. I'm just testing in FF right now, and it tells me that it's a syntax error. And with [for (x of obj) x] it tells me that obj isn't an iterator. Do you know if it's going to be?
What a lovely, in-depth, well formatted answer. Kudos to you, sir.
It's worth noting that Array comprehensions have been removed from the ES6 proposal in August and are now apparently planned to be included in ES7.
1

You could do this, in newer Browsers:

Object.defineProperty(Object.prototype, 'values', {
  get:function(){
    return function(o){
      var a = [];
      for(var i in o){
        a.push(o[i]);
      }
      return a;
    }
  }
});
var arrayOfValues = Object.values({a:'A',b:'B',c:'C'});

Really, I would just do:

function objectValues(obj, inherited){
  var a = [];
  for(var i in obj){
    var v = obj[i];
    if(inherited){
      a.push(v);
    }
    else if(obj.hasOwnProperty(i)){
      a.push(v);
    }
  }
  return a;
}
var notInheritedArrayOfValues = objectValues({a:'A',b:'B',c:'C'});
var inheritedArrayOfValues = objectValues({a:'A',b:'B',c:'C'}, true);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.