Skip to content

Instantly share code, notes, and snippets.

@frane
Created December 31, 2011 02:19
Show Gist options
  • Save frane/1542526 to your computer and use it in GitHub Desktop.
Save frane/1542526 to your computer and use it in GitHub Desktop.
Traversing arrays and objects in CoffeeScript
# Traversing arrays and objects in CoffeeScript
# The array and object we use for testing
arr = [1, 2, 3, 4, 5]
obj = {a: 1, b: 2, c: 3, d: 4, e: 5}
# 'in' has a different meaning in CoffeeScript than in JavaScript
# CS: element in array -> JS: array.indexOf(element) >= 0
console.log '5 in arr: ' + (5 in arr)
console.log '5 in obj: ' + (5 in obj)
console.log 'e in obj: ' + ('e' in obj)
# 'of' in CoffeeScript is the 'in' known in JavaScript
# CS: key of object -> JS: key in object
console.log '5 of arr: ' + (5 of arr)
console.log '5 of obj: ' + (5 of obj)
console.log 'e of obj: ' + ('e' of obj)
# Traverse through an array
# CS: for i in arr -> JS: for( var i; i < arr.length; i++ )
# CS: i -> JS: arr[i]
console.log 'i in arr'
for i in arr
console.log i
# CS: i -> JS: i, where i is an index
# This will return only results for keys that
# are numbers and within the range of the index
console.log 'i in obj'
for i in obj
console.log i
# CS: for i of arr -> JS for( i in arr )
# CS: i -> JS: i, where i is an object key
# Basically, the same as JS: for( i in Object.keys(arr) )
console.log 'i of arr'
for i of arr
console.log i
# This is the CS version of JS' for( k in obj )
console.log 'k of obj'
for k of obj
console.log k
# CS: k, v -> JS: k, obj[k]
console.log 'k, v of obj'
for k, v of obj
console.log k + ': ' + v
# array.forEach is a method and thus behaves consistently
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
console.log 'array.forEach(callback(v, k, arr))'
arr.forEach (element, index, array) ->
if index == 0
console.log 'arr == array: ' + (arr == array)
console.log index + ': ' + element
# .forEach is only available for arrays, not for objects
(function() {
var arr, i, k, obj, v, _i, _j, _len, _len2,
__indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
arr = [1, 2, 3, 4, 5];
obj = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5
};
console.log('5 in arr: ' + (__indexOf.call(arr, 5) >= 0));
console.log('5 in obj: ' + (__indexOf.call(obj, 5) >= 0));
console.log('e in obj: ' + (__indexOf.call(obj, 'e') >= 0));
console.log('5 of arr: ' + (5 in arr));
console.log('5 of obj: ' + (5 in obj));
console.log('e of obj: ' + ('e' in obj));
console.log('i in arr');
for (_i = 0, _len = arr.length; _i < _len; _i++) {
i = arr[_i];
console.log(i);
}
console.log('i in obj');
for (_j = 0, _len2 = obj.length; _j < _len2; _j++) {
i = obj[_j];
console.log(i);
}
console.log('i of arr');
for (i in arr) {
console.log(i);
}
console.log('k of obj');
for (k in obj) {
console.log(k);
}
console.log('k, v of obj');
for (k in obj) {
v = obj[k];
console.log(k + ': ' + v);
}
console.log('array.forEach(callback(v, k, arr))');
arr.forEach(function(element, index, array) {
if (index === 0) console.log('arr == array: ' + (arr === array));
return console.log(index + ': ' + element);
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment