-1

Given an object

  var obj = {0: "a", 1: "b", 2: "c"};

and expected result

  var arr = ["a", "b", "c"];

or an ArrayLikeObject

  var divs = document.querySelectorAll("div");

with expected result

  var arr = [<div>a</div>, <div>b</div>, <div>c</div>];

The expected results could be returned using $.map() as described at

Question:

What is the simplest, briefest approach using jQuery to convert an object or an ArrayLikeObject to an array ?

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177

3 Answers3

1

Use $.merge()

jQuery.merge( first, second )

first
Type: ArrayLikeObject
The first array-like object to merge, the elements of second added.

second
Type: ArrayLikeObject
The second array-like object to merge into the first, unaltered.

var obj = {0: "a", 1: "b", 2: "c"};
var arr = $.merge([], obj);
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

var divs = document.querySelectorAll("div");
var arr = $.merge([], divs);
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>a</div>
<div>b</div>
<div>c</div>

Alternatively, you could use $.extend()

var obj = {0: "a", 1: "b", 2: "c"};
var arr = $.extend([], obj);
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

var divs = document.querySelectorAll("div");
var arr = $.extend([], divs);
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>a</div>
<div>b</div>
<div>c</div>
guest271314
  • 1
  • 15
  • 104
  • 177
1

With array-like objects, you can use jQuery.makeArray(). (Also, with Array.from().)

var divs = document.querySelectorAll("div");
var arr = jQuery.makeArray(divs);

function foo() {
    return jQuery.makeArray(arguments);
}

foo(1, 'bar', false); // [1, 'bar', false]

It can also nearly work for the 1st object, but it'll need to be given a length (set to 1 + the greatest, positive, integer property).

var obj = { 0: "a", 1: "b", 2: "c", length: 3 };
var arr = jQuery.makeArray(obj);

If the length is unknown, you could figure it out first (ref: 9.4.2):

var obj = { 0: "a", 1: "b", 2: "c" };

obj.length = Object.keys(obj).reduce(function (prev, key) {
    var index = parseInt(key, 10);

    // 9.4.2 Array Exotic Objects
    // A String property name P is an array index if and only if...
    // ... ToString(ToUint32(P)) is equal to P...
    var isInteger = key === String(index);
    // ... and ToUint32(P) is not equal to 232−1.
    var isUInt32 = index >= 0 && index < Math.pow(2, 32) - 1;

    return isInteger && isUInt32 ? index + 1 : prev;
}, 0);

var arr = jQuery.makeArray(obj);

Or, let the engine figure it out, using jQuery.merge() with an array and the obj as already suggested.

Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • You could use `Object.keys()` where `.length` is unknown `var obj = { 0: "a", 1: "b", 2: "c", length: !function() {return Object.keys(this).length }() };` – guest271314 Feb 29 '16 at 05:13
  • What do you mean by _"(Now standardized as Array.from().)"_ ? The `.reduce()` callback is interesting; will have to review `isInteger` and `isUInt32` variables further – guest271314 Feb 29 '16 at 05:24
  • @guest271314 `Array.from()` was defined by ES6 as a native function in JavaScript and has the same behavior as `jQuery.makeArray()`. So, you can perform the same task with or without jQuery in supporting engines. – Jonathan Lonowski Feb 29 '16 at 05:28
0

Consider using Underscore:

_.values({0: "a", 1: "b", 2: "c"});

["a", "b", "c"]

ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
  • Note that the order of the values isn't guaranteed. Most modern engines do implement with some level of consistency, but the standard doesn't enforce that. So, it's valid if the keys or values shuffle. – Jonathan Lonowski Feb 29 '16 at 05:06
  • @JonathanLonowski - That's true... to guarantee order based on key order: `_.chain({0: "a", 1: "b", 2: "c"}).pairs().sortBy(([key]) => key).map(([, value]) => value).value();` – ic3b3rg Mar 01 '16 at 04:39