Built-in Methods to Iterate/Modify

Ruby

-Basic Iteration-

array = ["a", "b", "c", "d"]

.each

array.each {|x| puts x + "!"}

=> a! b! c! d!

.each.with_index

array.each.with_index {|x, i| puts "#{i}: #{x}"}

=> 0: a 1: b 2: c 3: d

.map

array.map {|x| x + "!"}

=> ["a!", "b!", "c!", "d!"]

-Selective Iteration-

.select

[1,2,3,4,5].select {|x| x.even?}

=> [2, 4]

.detect

[1,2,3,4,5].detect {|x| x.even?}

=> 2

.slice

p array.slice(1, 3)

=> ["b", "c", "d"] // up to, including

.include?

p array.include? "c"

=> true

wow Ruby is pretty straightforward

-Ordering and Sorting-

.sort

p [2, 13, 4, 22].sort

=> [2, 4, 13, 22]

.reverse

p array.reverse

=> ["d", "c", "b", "a"]

.max

p [1,2,3,4,5].max

=> 5

.min

p [1,2,3,4,5].min

=> 1

-Modification-

.reject!

p array.reject! {|x| x == "b"}

=> ["a","c","d"]

.insert(idx, obj)

array.insert(2, "z")

=> ['a', 'b', 'z', 'c', 'd']

.push

p array.push("e")

=> ["a", "b", "c", "d", "e"]

.pop

p array.pop

=> d

p array => ["a", "b", "c"]

.shift

p array.shift

=> a

.unshift

p array.unshift("$")

=> ["$", "a", "b", "c", "d"]

-String-Specific-

string = "Das Bootcamp"

.downcase

p string.downcase

=> das bootcamp

.upcase

p string.upcase

=> DAS BOOTCAMP

.ord

p string[0].ord

=> 68

.chr

return 65.chr, 66.chr, 67.chr

=> ["A", "B", "C"]

.sub

p string.sub(/boot/i, "Shoe")

=> Das Shoecamp

p "LalaLand".gsub(/la/i, "ba")

=> bababand // i - case insensitive

.scan

str = "We wake at 0600, dammit man!"

p str.scan(/[a-z]/i).join('')

=> Wewakeatdammitman

-OR- p str.scan(/[A-Za-z]/).join('')

JavaScript

-Basic Iteration-

var array = ["a", "b", "c", "d"];

.forEach()

array.forEach(function(x) {console.log(x,"!")});

=> a! b! c! d!

.forEach()

array.forEach(function(x, i) {console.log(i + ": " + x)});

=> 0: a 1: b 2: c 3: d

.map()

array.map(function(x) {return x + "!";});

=> ["a!", "b!", "c!", "d!"]

-Selective Iteration-

.filter()

[1,2,3,4,5].filter(function(x) {return (x % 2 === 0)});

=> [2, 4]

.find()

[1,2,3,4,5].find(function(x) {return x % 2 === 0});

=> 2

.slice()

console.log(array.slice(1, 3));

=> ["b", "c"] // up to, not including

.includes()

console.log(array.includes("c"));

TypeError: array.includes is not a function

// only has limited browser support currently

console.log(array.indexOf("c") != -1);

=> true

-Ordering and Sorting-

.sort()

console.log([2,13,4,22].sort(function(a, b) {return a - b}));

=> [2, 4, 13, 22]

.reverse()

console.log(array.reverse())

=> ["d", "c", "b", "a"]

// !! Always destructive in JS

Math.max

var max = Math.max.apply(null, [1,2,3,4,5]);

console.log(max);

=> 5

Math.min

var min = Math.min.apply(null, [1,2,3,4,5]);

console.log(min);

=> 1

-Modification-

delete

var index = array.indexOf("b");

delete array[index];

=> ["a", , "c", "d"]

.splice(idx, #removed, insertValue)

var anyRemoved = array.splice(2, 0, "z");

=> console.log(array);

['a', 'b', 'z', 'c', 'd']

console.log(anyRemoved);

=> []

.push()

var pushedArray = (array.push("e"));

console.log(pushedArray);

=> 5 // new length of array

console.log(array);

=> ['a', 'b', 'c', 'd', 'e']

.pop()

console.log(array.pop());

=> d

console.log(array); => ["a", "b", "c"]

.shift()

console.log(array.shift());

=> a

.unshift()

console.log(array.unshift("$"));

=> 5 // new length of array

console.log(array);

=> ["$", "a", "b", "c", "d"]

-String-Specific-

var string = "Das Bootcamp";

.toLowerCase()

console.log(string.toLowerCase());

=> das bootcamp

.toUpperCase()

console.log(string.toUpperCase());

=> DAS BOOTCAMP

.charCodeAt()

console.log(string.charCodeAt(0));

=> 68

.fromCharCode()

console.log(String.fromCharCode(65, 66, 67));

=> ABC //note capital "S" for object prototype

.replace(/regex/gim, newSubString)

console.log(string.replace(/boot/i, "Shoe"));

=> Das Shoecamp

// flags: i - ignore case, g - global match,

// m - match over multiple lines

.match()

var str = "We wake at 0600, dammit man!";

var newStr = str.match(/[a-z]/gi).join('');

console.log(newStr);

=> Wewakeatdammitman