lodashSandbox: dropRightWhile
ES6 Enabled
by David McClelland
HTML
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<div class="wrapper">
<div class="title">
<h3>
_.dropRightWhile
</h3>
</div>
<div class="subtitle">
<h4>
Remove elements from end of array while an evaluation is true of each element to be removed
</h4>
</div>
<div class="workspace">
<input id='startInput' type='text' value='[
{ "user": "barney", "active": true },
{ "user": "fred", "active": false },
{ "user": "pebbles", "active": false }
]' />
<label for="startInput">array</label>
<div class="actionArea">
<button id="button1">dropWhile</button>
<br />
<button id="button2">dropWhile shorthand</button>
<br />
<button id="button3">dropRightWhile</button>
<br />
<button id="button4">dropRightWhile shorthand</button>
<br />
</div>
<br />
<textarea id="output" rows="2" value="output"></textarea>
</div>
</div>
CSS
label {
font-size: 12px;
}
.wrapper {
display: flex;
flex-direction: column;
width: 500px;
}
.title,
.subtitle,
label {
font-family: Helvetica;
color: gray;
}
.workspace {
display: flex;
flex-direction: column;
}
.actionArea {
border: 1px solid #EEEEEE;
padding: 12px;
}
.actionArea button {
width: 200px;
}
#output {
font-family: Courier;
font-size: 14px;
}
#input1,
#input2 {
font-family: Courier;
font-size: 14px;
}
#modValInput {
font-family: Courier;
font-size: 14px;
}
Babel + JSX
// find elements
let banner = document.getElementById("#playground");
let button = document.getElementById("button");
let startInput = document.getElementById('startInput');
let output = document.getElementById("output");
button1.onclick = () => {
doOperation1();
}
button2.onclick = () => {
doOperation2();
}
button3.onclick = () => {
doOperation3();
}
button4.onclick = () => {
doOperation4();
}
function doOperation1() {
let inputVal = JSON.parse(startInput.value);
output.value = JSON.stringify(_.dropWhile(inputVal, (o) => {
return !o.active;
}));
}
function doOperation2() {
let inputVal = JSON.parse(startInput.value);
output.value = JSON.stringify( _.dropWhile(inputVal, { 'user': 'pebbles', 'active': false }));
}
function doOperation3() {
let inputVal = JSON.parse(startInput.value);
output.value = JSON.stringify(_.dropRightWhile(inputVal, (o) => {
return !o.active;
}));
}
function doOperation4() {
let inputVal = JSON.parse(startInput.value);
output.value = JSON.stringify( _.dropRightWhile(inputVal, { 'user': 'barney', 'active': false }));
}