Skip to content

Instantly share code, notes, and snippets.

Created November 8, 2015 18:29
Show Gist options
  • Save anonymous/c9ff22330f910ad836bf to your computer and use it in GitHub Desktop.
Save anonymous/c9ff22330f910ad836bf to your computer and use it in GitHub Desktop.
JavaScript Filter, Map and ForEach Example // source http://jsbin.com/nimatu
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
<script src="https://rawgit.com/angular/bower-angular/master/angular.min.js"></script>
<meta charset="utf-8">
<title>JavaScript Filter, Map and ForEach Example</title>
</head>
<body>
<div class="container">
<div class="content" ng-controller="MainCtrl">
<ul ng-repeat="student in students">
<li> <h1>{{ student }}</h1></li>
</ul>
</div>
</div>
<script id="jsbin-javascript">
console.clear();
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope, $rootScope) {
$scope.students = [];
var students = [
{ name: 'John Connor', score: 76, subject: 'biology' },
{ name: 'Mary Jones', score: 62, subject: 'history' },
{ name: 'James Young', score: 42, subject: 'physics' },
{ name: 'Susan Torpe', score: 46, subject: 'maths' }
];
var failed = students.filter(function(student) {
// filter students with a score less than 50%
return student.score < 50;
}).map(function(student) {
// find out which subjects the students failed
return student.name;
}).forEach(function(student) {
// print the names of the students that failed
console.log(student);
$scope.students.push(student);
});
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">console.clear();
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope, $rootScope) {
$scope.students = [];
var students = [
{ name: 'John Connor', score: 76, subject: 'biology' },
{ name: 'Mary Jones', score: 62, subject: 'history' },
{ name: 'James Young', score: 42, subject: 'physics' },
{ name: 'Susan Torpe', score: 46, subject: 'maths' }
];
var failed = students.filter(function(student) {
// filter students with a score less than 50%
return student.score < 50;
}).map(function(student) {
// find out which subjects the students failed
return student.name;
}).forEach(function(student) {
// print the names of the students that failed
console.log(student);
$scope.students.push(student);
});
});</script></body>
</html>
console.clear();
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope, $rootScope) {
$scope.students = [];
var students = [
{ name: 'John Connor', score: 76, subject: 'biology' },
{ name: 'Mary Jones', score: 62, subject: 'history' },
{ name: 'James Young', score: 42, subject: 'physics' },
{ name: 'Susan Torpe', score: 46, subject: 'maths' }
];
var failed = students.filter(function(student) {
// filter students with a score less than 50%
return student.score < 50;
}).map(function(student) {
// find out which subjects the students failed
return student.name;
}).forEach(function(student) {
// print the names of the students that failed
console.log(student);
$scope.students.push(student);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment