jquery convert object to array

By Sam Deering

Some options for converting a jQuery object into an array. Also you can use a jQuery object much like an array. For example:

obj = $(‘p’); // Get all paragraphs into a jQuery object
for (i = 0; i < obj.length; i++) { // Do something with obj[i] } [/js] As you can see, jQuery objects have .length and []. But, .pop() and .reverse() are missing. And you may want to pass the array to a function that expects a native array. So do this: [js] obj = $('p'); // Get all paragraphs a = $.makeArray(obj); // One way to do it a = obj.toArray(); // The other way [/js] also try: [js] $.each(piv, function(name, value) { console.log(name, value); }); or this for (var key in piv) { // var obj = piv[key]; console.log(key, piv[key]); } [/js] Also see:

  • kvvaradha

    hi sam, i need to push array of objects to an array . help me with sample code or tut with sample code,

    thanks in advance.



Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.