The jQuery's toArray()
method works on DOM elements in a wrapped set and returns a plain array of them. It would be handy if such a method worked also on JavaScript objects. Unfortunately, it does not. For that reason, I've implemented a global jQuery method which takes an object and returns an array containing its properties and their corresponding values. With very poor imagination I called it toArrayFromObject()
. Its code is as follows:
(function($) { $.toArrayFromObject = function(obj) { var arr = []; $.each(obj, function(property, value) { arr.push(property); arr.push(value); }); return arr; }; })(jQuery);
A simple test:
var Class = { type: 'Test', property: 'Foo' }; alert($.toArrayFromObject(Class)); // alerts type,Test,property,Foo