Loading...
More JavaScript Posts
/*** @param {number[]} nums* @param {number} target* @return {number[]}*/var twoSum = function(nums, target) {for(let i = 0; i < nums.length; i++) {for(let j = 0; j < nums.length; j++) {if(nums[i] + nums[j] === target && i !== j) {return [i, j]}}}};
const flat = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a));// Orconst flat = arr => arr.reduce((a, b) => Array.isArray(b) ? [...a, ...flat(b)] : [...a, b], []);// Or// See the browser compatibility at https://caniuse.com/#feat=array-flatconst flat = arr => arr.flat();// Exampleflat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']
const getTimezone = () => Intl.DateTimeFormat().resolvedOptions().timeZone;// ExamplegetTimezone();
var arr = [{"success": true,"data": [{"id": "ipi_1KrrbvDOiB2klwsKhuqUWqt1","object": "issuing.transaction","amount": -2743,"amount_details": {"atm_fee": null},"authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd","balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi","card": "ic_1Krqe5DOiB2klwsK44a35eiE","cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr","created": 1650753567,"currency": "usd","dispute": null,"livemode": false,"merchant_amount": -2743,"merchant_currency": "usd","merchant_data": {"category": "advertising_services","category_code": "7311","city": "San Francisco","country": "US","name": "Aeros Marketing, LLC","network_id": "1234567890","postal_code": "94103","state": "CA"},"metadata": {},"type": "capture","wallet": null},{"id": "ipi_1Krrbvc62B2klwsKhuqUWqt1","object": "issuing.transaction","amount": -9999,"amount_details": {"atm_fee": null},"authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd","balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi","card": "ic_1Krqe5DOiB2klwsK44a35eiE","cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr","created": 1650753567,"currency": "USD","dispute": null,"livemode": false,"merchant_amount": -9999,"merchant_currency": "usd","merchant_data": {"category": "fast_food","category_code": "7311","city": "San Francisco","country": "US","name": "Aeros Marketing, LLC","network_id": "1234567890","postal_code": "94103","state": "CA"},"metadata": {},"type": "capture","wallet": null}]}];const reduced = arr.reduce((prev, curr) => {prev.push({amount: curr.data[0].merchant_amount,id: curr.data[0].id,currency: curr.data[0].currency,category: curr.data[0].merchant_data.category});console.log(prev)return prev;}, []);
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;// ExamplescelsiusToFahrenheit(15); // 59celsiusToFahrenheit(0); // 32celsiusToFahrenheit(-20); // -4fahrenheitToCelsius(59); // 15fahrenheitToCelsius(32); // 0
class TreeNode {constructor(key, value = key, parent = null) {this.key = key;this.value = value;this.parent = parent;this.children = [];}get isLeaf() {return this.children.length === 0;}get hasChildren() {return !this.isLeaf;}}class Tree {constructor(key, value = key) {this.root = new TreeNode(key, value);}*preOrderTraversal(node = this.root) {yield node;if (node.children.length) {for (let child of node.children) {yield* this.preOrderTraversal(child);}}}*postOrderTraversal(node = this.root) {if (node.children.length) {for (let child of node.children) {yield* this.postOrderTraversal(child);}}yield node;}insert(parentNodeKey, key, value = key) {for (let node of this.preOrderTraversal()) {if (node.key === parentNodeKey) {node.children.push(new TreeNode(key, value, node));return true;}}return false;}remove(key) {for (let node of this.preOrderTraversal()) {const filtered = node.children.filter(c => c.key !== key);if (filtered.length !== node.children.length) {node.children = filtered;return true;}}return false;}find(key) {for (let node of this.preOrderTraversal()) {if (node.key === key) return node;}return undefined;}}const tree = new Tree(1, 'AB');tree.insert(1, 11, 'AC');tree.insert(1, 12, 'BC');tree.insert(12, 121, 'BG');[...tree.preOrderTraversal()].map(x => x.value);// ['AB', 'AC', 'BC', 'BCG']tree.root.value; // 'AB'tree.root.hasChildren; // truetree.find(12).isLeaf; // falsetree.find(121).isLeaf; // truetree.find(121).parent.value; // 'BC'tree.remove(12);[...tree.postOrderTraversal()].map(x => x.value);// ['AC', 'AB']