lodashSandbox: differenceWith

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>
      _.differenceWith
    </h3>
  </div>
  <div class="subtitle">
    <h4>
      2 arrays into 1
    </h4>
  </div>
  <div class="workspace">
    <input id='input1' type='text' value='[{ "x":1, "y":2 }, { "x":2, "y":1 }]'/>
    <label for="input1">array</label>
    <br />
    <select id="modValInput">
      <option value='_EQUAL' default='true'>equal</option>
      <option value='_CEILING'>ceiling</option>
      <option value='_ROUND'>round</option>
    </select>
    <br />
    <input id='input2' type='text' value='[{ "x":1, "y":2}]' />
    <label for="input1">array</label>
    <br />
    <button id="button">Execute</button>
    <br />
    <input id="output" type="text" value="output" />
  </div>
</div>

CSS

label {
  font-size: 12px;
}

.wrapper {
  display: flex;
  flex-direction: column;
}

.title, .subtitle, label {
  font-family: Helvetica;
  color: gray;
}

.workspace {
  display: flex;
  flex-direction: column;
  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 modValInput = document.getElementById('modValInput');
let firstArray = document.getElementById('input1');
let secondArray = document.getElementById('input2');
let output = document.getElementById("output");

// handle click and add class
button.onclick = () => {
  doOperation();
}

function doOperation() {
first = JSON.parse(firstArray.value);
second = JSON.parse(secondArray.value);
modValName = modValInput.value;
switch (modValName) {
	case '_EQUAL' :
  modVal = _.isEqual;
  break;
case '_CEILING' :
modVal = Math.ceiling;
break;
case '_ROUND' :
modVal = Math.round;
break;
}
  output.value = JSON.stringify(_.differenceWith(first, second, modVal));
}