{ jQuery foreach }

There is technically no cross-browser compliant Javascript function for PHP’s foreach(), so we use jQuery.

If we have an array and need both the index (key) and value for each:

var arr = [];
arr[0] = 'First Value';
arr[1] = 'Second Value';

jQuery.each(arr, function(key, val) {
     console.log(arr[key]);
});

Returns:
First Value
Second Value

If we have an object:

var object = {
   'key': 'value',
   'key2': 'value2'
};

jQuery.each(object, function(key, val) {
     console.log(object[key]);
});

Leave a Reply

Your email address will not be published. Required fields are marked *