// Setup
var arrayOfUsers = [{
id: 1,
username: "morris082",
logins: 3,
age: 30
}, {
id: 2,
username: "jimbob",
logins: 13,
age: 17
}, {
id: 3,
username: "supersally",
logins: 53,
age: 32
}];
// Filter users
// removing any user who is under 18
// return truthy to indicate the item remains in the array
var result = arrayOfUsers.filter(function (item) {
return item.age >= 18;
});
console.log(result);
// Map users
// apply modifications to each item
/*
var result = arrayOfUsers.map(function (item) {
item.points = 5;
return item;
});
console.log(result);
/**/
// Reduce users into a single value
// Lets get total logins
// using "optional initial value" to start at 0
/*
var totalLogins = arrayOfUsers.reduce(function (prev, current, i, arr) {
//console.group("Reduce iteration:");
//console.log(prev, current, i, arr);
//console.groupEnd();
return prev + current.logins;
}, 0);
console.log(totalLogins);
/**/
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.