Display key-value pair of associative array in Javascript with jQuery

Useful snippet for debugging javascript. It prints out key-value pairs of associative array. In PHP your have function print_r($array), but in Javascript you still have to make your own function.
1 2 3 4 5 |
$.each(myAssociativeArr, function (idx, obj) { $.each(this, function(key, val){ console.log(key, val) }) }) |
You can fill associative array, for example, this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Initialize array in Javascript var myAssociativeArr = []; //Add element to array myAssociativeArr.push( { id: 1, title: "my title 1", url: "http://url1", css: "myCssClass1", txtdescription: "my text description 1" }); myAssociativeArr.push( { id: 2, title: "my title 2", url: "http://url2", css: "myCssClass2", txtdescription: "my text description 2" }); |