<html>
<head>
<script src="//cdn.jsdelivr.net/pouchdb/5.4.4/pouchdb.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<h1>CHECK THE CONSOLE.</h1>
</body>
</html>
console.clear();
var db = new PouchDB('friendsdb');
//db.destroy();
var docs = [{
'_id': '1',
'number': 10,
'values': '1, 2, 3',
'loto': 'fooloto'
}, {
'_id': '2',
'number': 12,
'values': '4, 7, 9',
'loto': 'barloto'
}, {
'_id': '3',
'number': 13,
'values': '9, 4, 5',
'loto': 'fooloto'
}];
db.bulkDocs(docs).then(function(result) {
querySum();
queryLargest();
querySmallest();
queryAverage();
}).catch(function(err) {
console.log(err);
});
function querySum() {
function map(doc) {
// the function emit(key, value) takes two arguments
// the key (first) arguments will be sent as an array to the reduce() function as KEYS
// the value (second) arguments will be sent as an array to the reduce() function as VALUES
emit(doc._id, doc.number);
}
function reduce(keys, values, rereduce) {
// keys:
// here the keys arg will be an array containing everything that was emitted as key in the map function...
// ...plus the ID of each doc (that is included automatically by PouchDB/CouchDB).
// So each element of the keys array will be an array of [keySentToTheEmitFunction, _idOfTheDoc]
//
// values
// will be an array of the values emitted as value
console.info('keys ', JSON.stringify(keys));
console.info('values ', JSON.stringify(values));
// check for more info: http://couchdb.readthedocs.io/en/latest/couchapp/views/intro.html
// So, since we want the sum, we can just sum all items of the values array
// (there are several ways to sum an array, I'm just using vanilla for to keep it simple)
var i = 0, totalSum = 0;
for(; i < values.length; i++){
totalSum += values[i];
}
return totalSum;
}
db.query({map: map, reduce: reduce}, function(err, response) {
console.log('sum is ' + response.rows[0].value);
});
}
function queryLargest() {
function map(doc) {
emit(doc._id, doc.number);
}
function reduce(keys, values, rereduce) {
// everything same as before (see querySum() above)
// so, this time we want the larger element of the values array
// http://stackoverflow.com/a/1379560/1850609
return Math.max.apply(Math, values);
}
db.query({map: map, reduce: reduce}, function(err, response) {
console.log('largest is ' + response.rows[0].value);
});
}
function querySmallest() {
function map(doc) {
emit(doc._id, doc.number);
}
function reduce(keys, values, rereduce) {
// all the same... now the looking for the min
return Math.min.apply(Math, values);
}
db.query({map: map, reduce: reduce}, function(err, response) {
console.log('smallest is ' + response.rows[0].value);
});
}
function queryAverage() {
function map(doc) {
emit(doc._id, doc.number);
}
function reduce(keys, values, rereduce) {
// now simply calculating the average
var i = 0, totalSum = 0;
for(; i < values.length; i++){
totalSum += values[i];
}
return totalSum/values.length;
}
db.query({map: map, reduce: reduce}, function(err, response) {
console.log('average is ' + response.rows[0].value);
});
}
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |