I have a few I always use in my applications these days. Cloning things is something I find myself doing routinely in single page applications and comparing values. I could list way more, but I stopped at three, it was hard just choosing one.
Cloning an array:
function cloneArray(array) {
return array.slice();
}
Cloning an object:
function cloneObject(obj) {
return JSON.parse(JSON.stringify(obj));
}
Object comparison:
For comparing if two objects are the same. Loops through and checks if their properties match, their lengths and so on.
function objectIsEqual(a, b) {
var aProps = Object.keys(a);
var bProps = Object.keys(b);
if (aProps.length !== bProps.length) {
return false;
}
for (let i = 0; i < aProps.length; i++) {
var propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
}