Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Array.prototype.slice.call(arrayLikeObject)</code></p> <p>is the standard way to convert and an array-like object to an array. </p> <p>That only really works for the <code>arguments</code> object. To convert a generic object to an array is a bit of a pain. Here's the source from <a href="http://documentcloud.github.com/underscore/docs/underscore.html#section-29" rel="noreferrer">underscore.js</a>:</p> <pre><code>_.toArray = function(iterable) { if (!iterable) return []; if (iterable.toArray) return iterable.toArray(); if (_.isArray(iterable)) return iterable; if (_.isArguments(iterable)) return slice.call(iterable); return _.values(iterable); }; _.values = function(obj) { return _.map(obj, _.identity); }; </code></pre> <p>Turns out you're going to need to loop over your object and map it to an array yourself. </p> <pre><code>var newArray = [] for (var key in object) { newArray.push(key); } </code></pre> <p>You're confusing the concepts of arrays and "associative arrays". In JavaScript, objects kind of act like an associative array since you can access data in the format <code>object["key"]</code>. They're not real associative arrays since objects are unordered lists.</p> <p>Objects and arrays are vastly different. </p> <p>An example of using underscore:</p> <pre><code>var sortedObject = _.sortBy(object, function(val, key, object) { // return an number to index it by. then it is sorted from smallest to largest number return val; }); </code></pre> <p>See <a href="http://jsfiddle.net/Raynos/rNvFy/1/" rel="noreferrer">live example</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload