Skip to content

Instantly share code, notes, and snippets.

@raunaqsachdev
raunaqsachdev / uniqueValuesSet.js
Last active March 18, 2018 12:05
Unique values in an array using Set
const sampleValues = [1, 4, 5, 2, 'a', 'e', 'b', 'e', 2, 2, 4];
const uniqueValues = [...new Set(sampleValues)];
console.log(uniqueValues); //[1, 4, 5, 2, "a", "e", "b"]
@raunaqsachdev
raunaqsachdev / arrayUnique.js
Created March 18, 2018 11:47
Finding unique values in an array
const unique = (value, index, self) => {
return self.indexOf(value) === index;
}
const sampleValues = [1, 4, 5, 2, 'a', 'e', 'b', 'e', 2, 2, 4];
const uniqueValues = sampleValues.filter(unique);
@raunaqsachdev
raunaqsachdev / switchHashMap.js
Last active February 11, 2018 07:41
JavaScript Hash map
var switchObject2 = Object.assign(Object.create(null), {
A : function1,
B : function2,
default: defaultFunction,
});
/* OR
var switchObject2 = Object.create(null);
switchObject2['A'] = function1;
@raunaqsachdev
raunaqsachdev / switchObject.js
Created February 11, 2018 05:35
Object instead of switch
var switchObject1 = {
A : function1,
B : function2,
default: defaultFunction,
}
var resultFunction = switchObject1[valueStore] || switchObject1['default'];
resultFunction();
@raunaqsachdev
raunaqsachdev / switch.js
Created February 11, 2018 05:09
Switch condition
switch(valueStore) {
case A: {
function 1();
break;
}
case B: {
function2();
break;
}
default: {
@raunaqsachdev
raunaqsachdev / objectSpread.js
Last active January 28, 2018 15:17
Object spread operator
let obj1 = {
a: 1,
b: 2,
c: 3
};
let obj2 = {
p: 4,
q: 5,
r: 6
@raunaqsachdev
raunaqsachdev / functionArgDestructuring.js
Last active October 30, 2018 22:22
Using the destructuring in function arguments
function printName({firstName, lastName}) {
console.log(`Name : ${firstName} ${lastName}`);
};
let personObj = {
firstName: 'John',
lastName: 'Doe'
};
printName(personObj); //Name : John Doe
@raunaqsachdev
raunaqsachdev / destructureObjectsES5.js
Created January 28, 2018 05:18
ES5 equivalent to destructuring objects in JS
var sampleObj = {
firstName: John,
lastName: Doe,
age: 29,
gender: male
};
var name = sampleObj.name, //undefined
firstName = sampleObj.firstName, //John
lastName = sampleObj.lastName, //Doe
@raunaqsachdev
raunaqsachdev / destructureObjects.js
Last active October 30, 2018 22:23
Object destructuring ES7
let sampleObj = {
firstName: 'John',
lastName: 'Doe',
age: 29,
gender: 'male'
};
let { name, firstName, lastName, gender:sex } = sampleObj;
//name = undefined, firstName = John, lastName = Doe, sex = male
@raunaqsachdev
raunaqsachdev / arraySpreadIterables.js
Last active January 28, 2018 14:41
Array spread works over all iterables
let [x,y,z] = 'a\uD83D\uDCA9c'; // x='a'; y='\uD83D\uDCA9'; z='c'
let [p, q] = new Set(['new', 'string']); // p='new'; q='string’;