List of useful function on javascript
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
//to use
const newRunningNumber = zeroPad((ticketNumExisting.length + 1),4); //e.g 0052
const has = function(obj, key) {
return key.split(".").every(function(x) {
if(typeof obj != "object" || obj === null || ! x in obj)
return false;
obj = obj[x];
return true;
});
}
//usage
const attachmentFile1= has(nextProps, 'attachmentFiles.issue.issue1') ?
nextProps.attachmentFiles.issue.issue1 : '',
function removeDuplicates(originalArray, objKey) {
var trimmedArray = [];
var values = [];
var value;
for(var i = 0; i < originalArray.length; i++) {
value = originalArray[i][objKey];
if(values.indexOf(value) === -1) {
trimmedArray.push(originalArray[i]);
values.push(value);
}
}
return trimmedArray;
}
//Data
[
{
"color": "red",
"size": "small"
},
{
"color": "blue",
"size": "medium"
},
{
"color": "red",
"size": "large"
}
]
//Usage
removeDuplicates(arrayWithDuplicates, 'size');
const checkedLineItems = this.state.newLineItems.filter((item) => {
return item.checkboxe === true;
});
console.log(checkedLineItems.length);