13

I have following object:

var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};

Is it possible to get values from this object without looping it ?

It is possible to use jQuery.

Expected result:

var output = [2, 6, 4];
hsz
  • 148,279
  • 62
  • 259
  • 315
  • Do you know objects keys? Are they static? – Kasyx May 24 '13 at 09:01
  • Is it ok if under-the-hood jQuery will use loops? – Dovydas Navickas May 24 '13 at 09:02
  • 7
    A loop is always going to be necessary on some level – Alex K. May 24 '13 at 09:03
  • Underscore JS has a function for that: http://underscorejs.org/#values – Markus Hedlund May 24 '13 at 09:09
  • 2
    If the keys are dynamic (i.e., unknown) then there is no way to do this without a loop. Note also that object properties do not have an inherent order, so there's no way to guarantee the output array would have the values in the order you show. (You can extract the keys from an object and sort them and then produce the final output based on that, but the output you showed isn't based on alphabetic order, so...) – nnnnnn May 24 '13 at 09:11

5 Answers5

8
var arr = $.map(input,function(v){
 return v;
});

Demo --> http://jsfiddle.net/CPM4M/

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
8

This is simply not possible without a loop. There's no Object.values() method (yet) to complement Object.keys().

Until then you're basically "stuck" with the below construct:

var values = [];

for (var k in input) {
  if (input.hasOwnProperty(k)) {
    values.push(input[k]);
  }
}

Or, in modern browsers (but of course still using a loop and an anonymous function call):

var values = Object.getOwnPropertyNames(input).map(function(key) {
    return input[key];
});
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • 1
    Now there is Object.values() but it's still at its experimental stage with Google chrome 51.0 and Firefox 47 supporting the function. Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values – Tolani Sep 14 '16 at 12:44
  • `Object.values()` is now standard in all latest browsers (including mobile), with the exception of the obsolete IE (all versions). – Studocwho Nov 02 '19 at 23:35
4

You can get values from this object without looping using Object.values() method like:

var output = Object.values( input );
console.log( output );  // [2, 6, 4]

DEMO:

var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};

var output = Object.values( input );
console.log( output );

PLEASE NOTE:

This is an experimental technology

Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

So, currently it supports Chrome & Firefox only.

Community
  • 1
  • 1
palaѕн
  • 72,112
  • 17
  • 116
  • 136
3

I don't know why you want without loop . here my solution

JSON.stringify( input ).replace(/"(.*?)"\:|\{|\}/g,'' ).split(',')

it print [2, 6, 4] . I didn't test for other json values

rab
  • 4,134
  • 1
  • 29
  • 42
  • This regex only works on key-value pairs where the value is not quoted because it greedily absorbs values as well. So it will work with the OP's example but it won't generalize. – Dean Stamler Feb 13 '18 at 21:44
-2
var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};
var newArr = new Array;
$.each(input,function(key,value) {
    newArr.push(value);
});
alert(newArr)
Manish Jangir
  • 505
  • 3
  • 9