Skip to content

Instantly share code, notes, and snippets.

@IngIeoAndSpare
Created July 30, 2019 02:55
Show Gist options
  • Save IngIeoAndSpare/30f20b0a587eb7f4c047cba343ab5e0a to your computer and use it in GitHub Desktop.
Save IngIeoAndSpare/30f20b0a587eb7f4c047cba343ab5e0a to your computer and use it in GitHub Desktop.
some tip javaScript.
// 1. console log
var tile_3000 = { id : 'id_1', count : 20, vt : true};
var tile_4000 = { id : 'id_2', count : 40, vt : true};
var tile_5000 = { id : 'id_3', count : 30, vt : false};
// print object name
// set css
console.log('========= css ==============');
console.log(`%c tile info`, `color: orange; font-weight : bold;`); //on browser
console.log({tile_3000, tile_4000, tile_5000});
// console table
console.log('========== table ============');
console.table([tile_3000, tile_4000, tile_5000]);
// 2. use key
console.log('============ use key ============');
const tileInfo = {
name : 'tile_5000',
vt : false,
size : 65,
coordinate : 'openGL',
zoom : 1
}
function printInfo ({name, coordinate, zoom}){
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
// not {{'}}, use {{`}}
var infoText = `tile ${name} coordinate is ${coordinate}. zoom => ${zoom}`;
console.log(infoText);
}
// or
function printInf(tileInfo){
const {name, coordinate, zoom} = tileInfo;
var infoText = `tile ${name} coordinate is ${coordinate}. zoom => ${zoom}`;
console.log(infoText);
}
printInf(tileInfo);
// 3. push array
console.log('============ array ============');
var testArray = ['tile_3000', 'tile_4000', 'tile_5000']
testArray = [...testArray, 'tile_6000', 'tile_7000']
console.log(`add => ${testArray}`);
// or shift
testArray = ['tile_6000', ...testArray, 'tile_7000']
console.log(`shift => ${testArray}`);
// 4. loop
console.log('============ loop ============');
const orders = [500, 30, 99, 15, 223];
//reduce
const total = orders.reduce((acc, cur) => acc + cur);
console.log(`reduce use => ${total}`);
//map value * 1.1
const withTax = orders.map(value => value * 1.1);
console.log(`map use => ${withTax}`);
// filter (if value > 100 ? push)
const highValue = orders.filter(value => value > 100);
console.log(`use filter => ${highValue}`);
// 5. await
const random = () => {
return Promise.resolve(Math.random());
}
const asyncNum = async() => {
const number_1 = await random();
const number_2 = await random();
console.log(`result ${number_1} and ${number_2}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment