A little loopy

Code examples for loops, jQuery.each(), ES5 Array methods and lodash. Link: http://intel.to/loopy


var rebels = [
	{
		"name": "Yoda",
		"age": 896,
		"height": 0.66,
		"home": "Dagobah",
		"sex": "male",
		"weapons": ["lightsaber","force"],
		"fight": function() {
			console.log('Die you will!');
		}
	},	
	{
		"name": "Luke Skywalker",
		"age": 19,
		"height": 1.72,
		"home": "Tatooine",
		"sex": "male",
		"weapons": ["lightsaber","blaster"],
		"fight": function() {
			console.log('Dodge this!');
		}
	},
	{
		"name": "Han Solo",
		"age": 29,
		"height": 1.8,
		"home": "Corellia",
		"sex": "male",
		"weapons": ["blaster","fist"],
		"fight": function() {
			console.log('Eat my blaster!');
		}
	},
	{
		"name": "Leia Organa",
		"age": 19,
		"height": 1.5,
		"home": "Alderaan",
		"sex": "female",
		"weapons": ["blaster","diplomacy"],
		"fight": function() {
			console.log('Pow!');
		}
	},	
	{
		"name": "Chewbacca",
		"age": 200,
		"height": 2.28,
		"home": "Kashyyyk",
		"sex": "male",
		"weapons": ["blaster","smell"],
		"fight": function() {
			// http://starwars.wikia.com/wiki/Shyriiwook
			// "Why did you pick on someone one fourth your size?"
			console.log('Waa hu aa ma ma a oo gah?');
		}
	},
	{
		"name": "Beyoncé Knowles",
		"age": 32,
		"height": 1.69,
		"home": "Urthha (Earth)",
		"sex": "female",
		"weapons": ["hips"],
		"fight": function() {
			console.log('Diva power!');
		}
	}
];

function metersToFeet(meters) {
	var converted = meters * 3.28084;
	var feet = Math.floor(converted);
	var inches = Math.floor( (converted - feet) * 12 );
	return feet + '\'' + inches + '"';
}

var planets = {
	"Dagobah": {
		"diameter": 8900,
		"url": "http://starwars.wikia.com/wiki/Dagobah",
		"moons": 1
	},
	"Tatooine": {
		"diameter": 10465,
		"url": "http://starwars.wikia.com/wiki/Tatooine",
		"moons": 3
	},
	"Kashyyyk": {
		"diameter": 12765,
		"url": "http://starwars.wikia.com/wiki/Kashyyyk",
		"moons": 3
	},
	"Alderaan": {
		"diameter": 12500,
		"url": "http://starwars.wikia.com/wiki/Alderaan",
		"moons": 1
	},
	"Corellia": {
		"diameter": 11000,
		"url": "http://starwars.wikia.com/wiki/Corellia",
		"moons": 3
	},
	"Urthha": {
		"diameter": 12742,
		"url": "http://starwars.wikia.com/wiki/Earth_%28planet%29",
		"moons": 1
	}
};
/ {{numSteps}}
Use For Loop jQuery ES5 lodash
Do something with each member

for (var i = 0; i < rebels.length; i++) {
  rebels[i].feet = metersToFeet(rebels[i].height);
}			
		

$.each(rebels, function(i, person) {
  person.feet = metersToFeet(person.height);
});
		

rebels.forEach(function(person) {
  person.feet = metersToFeet(person.height);
});
		

_.forEach(rebels, function(person) {
  person.feet = metersToFeet(person.height);
});
		
Gather

var heights = [];
for (var i = 0; i < rebels.length; i++) {
  heights.push(rebels[i].feet);
}			
		

var heights = [];			
$.each(rebels, function(i, person) {
  heights.push(person.feet);
});
		

var heights = rebels.map(function(person) {
  return person.feet;
});
		

var heights = _.pluck(rebels, 'feet');
		
Find some

var girlRebels = [];
for (var i = 0; i < rebels.length; i++) {
  if (rebels[i].sex == 'female') {
    girlRebels.push(rebels[i]);
  }
}			
		

var girlRebels = [];			
$.each(rebels, function(i, person) {
  if (person.sex == 'female') {
    girlRebels.push(person);
  }
});
		

var girlRebels = rebels.filter(function(person) {
  return (person.sex == 'female');
});
		

var girlRebels = _.where(rebels, {sex:'female'});
		
Find some - complex

var jedis = [];
for (var i = 0; i < rebels.length; i++) {
  if (rebels[i].weapons.indexOf('lightsaber') > -1) {
    jedis.push(rebels[i]);
  }
}			
		

var jedis = [];			
$.each(rebels, function(i, person) {
  if (person.weapons.indexOf('lightsaber') > -1) {
    jedis.push(person);
  }
});
		

var jedis = rebels.filter(function(person) {
  return (person.weapons.indexOf('lightsaber') > -1);
});
		

var jedis = _.where(rebels, {weapons:['lightsaber']});
		
Find the first

var girl;
for (var i = 0; i < rebels.length; i++) {
  if (rebels[i].sex == 'female') {
    girl = rebels[i];
    break;
  }
}		
		

var girl;		
$.each(rebels, function(i, person) {
  if (person.sex == 'female') {
    girl = person;
    return false;
  }
});
		

var girl;
rebels.some(function(person) {
  if (person.sex == 'female') {
    girl = person;
    return true;
  }
});
		

var girl = _.find(rebels, {sex:'female'});
		
Are there any?

var hasGirl = false;
for (var i = 0; i < rebels.length; i++) {
  if (rebels[i].sex == 'female') {
    hasGirl = true;
    break;
  }
}		
		

var hasGirl = false;			
$.each(rebels, function(i, person) {
  if (person.sex == 'female') {
    hasGirl = true;
    return false;
  }
});
		

var hasGirl = rebels.some(function(person) {
	return (person.sex == 'female');
});
		

var hasGirl = !!_.find(rebels, {sex:'female'});
		
Total up

var meters = 0;
for (var i = 0; i < rebels.length; i++) {
  meters += rebels[i].height;
}		
		

var meters = 0;	
$.each(rebels, function(i, person) {
  meters += person.height;
});
		

var meters = rebels.reduce(function(total, person) {
  return total + person.height;
}, 0);
		

var meters = _.reduce(rebels, function(total, person) {
  return total + person.height;
}, 0);
		
Find max

var maxAge = 0;
for (var i = 0; i < rebels.length; i++) {
  if (rebels[i].age > maxAge) {
    maxAge = rebels[i].age;
  }
}	
		

var maxAge = 0;	
$.each(rebels, function(i, person) {
  if (person.age > maxAge) {
    maxAge = person.age;
  }
});
		

var maxAge = rebels.reduce(function(max, person) {
  return Math.max(person.age, max);
}, 0);
		

var maxAge = _.max(rebels, 'age').age;
		
Build lookup

var lookup = {};
for (var i = 0; i < rebels.length; i++) {
  lookup[rebels[i].name] = rebels[i];
}		
		

var lookup = {};
$.each(rebels, function(i, person) {
  lookup[person.name] = person;
});
		

var lookup = rebels.reduce(function(lookup, person) {
  lookup[person.name] = person;
  return lookup;
}, {});
		

var lookup = _.indexBy(rebels, 'name');
		
Build grouped lookup

var lookup = {};
for (var i = 0; i < rebels.length; i++) {
  if (!lookup[rebels[i].age]) {
    lookup[rebels[i].age] = [];
  }
  lookup[rebels[i].age].push(rebels[i]);
}		
		

var lookup = {};
$.each(rebels, function(i, person) {
  if (!lookup[person.age]) {
    lookup[person.age] = [];
  }
  lookup[person.age].push(person);
});
		

var lookup = rebels.reduce(function(lookup, person) {
  if (!lookup[person.age]) {
    lookup[person.age] = [];
  }
  lookup[person.age].push(person);  
  return lookup;
}, {});
		

var lookup = _.groupBy(rebels, 'age');
		
Invocation

for (var i = 0; i < rebels.length; i++) {
  rebels[i].fight();
}		
		

$.each(rebels, function(i, person) {
  person.fight();
});
		

rebels.forEach(function(person) {
  person.fight();
});
		

_.invoke(rebels, 'fight');
		
Object Iteration

for (var name in planets) {
  if (!planets.hasOwnProperty(name)) {
    continue;
  }
  planets[name].feet = planets[name].diameter * 3.28084;
}		
		

$.each(planets, function(name, planet) {
  planet.feet = planet.diameter * 3.28084;
});
		

Object.keys(planets).forEach(function(name) {
  planets[name].feet = planets[name].diameter * 3.28084;
});
		

_.forEach(rebels, function(name, planet) {
  planet.feet = planet.diameter * 3.28084;
});
		
Object Filter

var large = [];		
for (var name in planets) {
  if (!planets.hasOwnProperty(name)) {
    continue;
  }
  if (planets[name].diameter > 12000) {
    large.push(planets[name]);
  }
}		
		

var large = [];			
$.each(planets, function(name, planet) {
  if (planet.diameter > 12000) {
    large.push(planet);
  }
});
		

var large = [];
Object.keys(planets).forEach(function(name) {
  if (planets[name].diameter > 12000) {
    large.push(planets[name]);
  }
});
		

var large = _.filter(planets, function(planet, name) {
  return (planet.diameter > 12000);
});
		
Object Find

var found;
for (var name in planets) {
  if (!planets.hasOwnProperty(name)) {
    continue;
  }
  if (planets[name].diameter == 11000) {
    found = name;
    break;
  }
}		
		

var found;		
$.each(planets, function(name, planet) {
  if (planet.diameter == 11000) {
    found = name;
    return false;
  }
});
		

var found;			
Object.keys(planets).some(function(name) {
  if (planets[name].diameter == 11000) {
    found = name;
    return true;
  }
});
		

var found = _.findKey(planets, {diameter: 11000});