最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Retrieving all values from a JavaScript object - Stack Overflow

programmeradmin2浏览0评论

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).

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).

Share Improve this question edited Nov 14, 2014 at 1:43 mido asked Nov 14, 2014 at 1:31 midomido 25.1k15 gold badges99 silver badges122 bronze badges 6
  • 5 for..in seems more suitable for objects. – Shomz Commented Nov 14, 2014 at 1:33
  • if you are using underscore - there is a values function. – Daniel A. White Commented Nov 14, 2014 at 1:35
  • Not in plain JavaScript, but it's simple to make. – StackSlave Commented Nov 14, 2014 at 1:38
  • 1 No. There is no built in way to do what you asked. – jfriend00 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... – mido Commented Nov 14, 2014 at 1:46
 |  Show 1 more ment

2 Answers 2

Reset to default 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 prehensions, 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 posed 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;
}

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);
发布评论

评论列表(0)

  1. 暂无评论