HomeToolsAbout a20k

Object Basics

Object Behaviors

Object with integer Keys are automatically sorted ascending

String keys are not sorted automatically

const integerKeyObject = { 100: "a", 2: "b", 7: "c" }; Object.keys(integerKeyObject); // ["2", "7", "100"] const stringKeyObject = { "c": "somestring", "a": 42, "b": false }; Object.keys(stringKeyObject); // ["c", "a", "b"]

Object Strategies

Cleanup guard clauses with lookup

// Guard Clauses function logicalCalc(array, operation) { let result = array[0]; for(let i = 1; i < array.length; i++) { if(operation === 'AND') { result = result && array[i]; } if(operation === 'OR') { result = result || array[i]; } if(operation === 'XOR') { result = result !== array[i]; } } return result; } logicalCalc([false, true, 3], 'AND'); // Object Key Lookup const operations = { AND: (a, b) => a && b, OR: (a, b) => a || b, XOR: (a, b) => a !== b, }; const logicalCalc = (array, options) => array.reduce(operations[options]); logicalCalc([1,2,3], 'AND');

Methods

.map()

Map can be used to get all specified keys of objects

const items = [ {name: "Bike", price: 100}, {name: "Dish", price: 50} ]; const itemNames = items.map( (item)=>{ return item.name; }); console.log(itemNames); // ["Bike", "Dish"]
© VincentVanKoh