underscore playground
filtering on arrays of JSO
by Fernando De Leon
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<button id="runme">
Run me
</button>
<button id="runme2">
Watch console!
</button>
<div class="row">
<div class="col-md-12">
Watch console!
<textarea id="code"></textarea>
</div>
</div>
<button id="sort">
Sort watch console
</button>
JavaScript
var sectionArr = [{
id:125,
name:"JS generated section"
},
{
id:200,
name:"General"
}
];
var cols = [ {
title: 'Date by JS',
sectionName: 'JS generated section',
type:'CI.iSheet.ReproduceSheet.properties.dateField',
mandatory: false
},
{
title: 'States radio button non mandatory',
sectionName: 'JS generated section',
type: 'CI.iSheet.ReproduceSheet.properties.choiceField',
choices: [{choice:' SA'},{choice:' QLD'},{choice:' WA'},{choice:' VIC'},{choice:' ACT'},{choice:' NT'},{choice:' TAS'}],
userSelection: false,
choiceType: 'CI.iSheet.ReproduceSheet.properties.choiceType.radioButtons',
mandatory: false
},
{
title: 'RadioButtons Choice',
sectionName: 'General',
type: 'CI.iSheet.ReproduceSheet.properties.choiceField',
choices: [{choice:' choice 1'},{choice:' choice 2'},{choice:' choice 3'}],
userSelection: false,
choiceType: 'CI.iSheet.ReproduceSheet.properties.choiceType.radioButtons',
mandatory: false
},
{
title: 'checkBoxes',
sectionName: 'General',
type: 'CI.iSheet.ReproduceSheet.properties.choiceField',
choices: [{choice:' choice 1'},{choice:' choice 2'},{choice:' choice 3'}],
userSelection: false,
choiceType: 'CI.iSheet.ReproduceSheet.properties.choiceType.checkboxes',
mandatory: false
},
{
title: 'sample Text ARea',
sectionName: 'General',
type: 'CI.iSheet.ReproduceSheet.properties.textAreaField',
mandatory: false
},
];
var unsorted = [
{num:2,numSt:"4",name:"a"},
{num:4,numSt:"5",name:"s"},
{num:5,numSt:"8",name:"2"},
{num:1,numSt:"9",name:"c"},
{num:7,numSt:"3",name:"w"},
];
$("#sort").on("click",function(e) {
var sortedNum = _.sortBy(unsorted,function(ele) {
return ele.num;
});
var sortedNumSt = _.sortBy(unsorted,function(ele) {
return ele.numSt;
});
var sortedName = _.sortBy(unsorted,function(ele) {
return ele.name;
});
console.log("sorted Num",sortedNum);
...