Skip to content

Instantly share code, notes, and snippets.

@awkale
Last active May 23, 2019 14:39
Show Gist options
  • Save awkale/b043fcaed767ca6d4ae910559829e0d9 to your computer and use it in GitHub Desktop.
Save awkale/b043fcaed767ca6d4ae910559829e0d9 to your computer and use it in GitHub Desktop.
ES6 #javascript #es6 #cheatsheet
// find an object in an array based on object property
const objArray = [
{ id: 0, name: 'Object 0', otherProp: '321' },
{ id: 1, name: 'O1', otherProp: '648' },
{ id: 2, name: 'Another Object', otherProp: '850' },
{ id: 3, name: 'Almost There', otherProp: '046' },
{ id: 4, name: 'Last Obj', otherProp: '984' }
];
let obj = objArray.find(function (obj) { return obj.id === 3; });
// shallow clone
let obj = {
a: 1,
b: 2,
};
let objCopy = Object.assign({}, obj);
console.log(objCopy);
// Result - { a: 1, b: 2 }
// deep clone
function cloneObject(obj) {
var clone = {};
for(var i in obj) {
if(obj[i] != null && typeof(obj[i])=="object") {
clone[i] = cloneObject(obj[i]);
} else {
clone[i] = obj[i];
}
}
return clone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment