Node.js Modules

Node.js Modules

// Use require.main.require to get a module from the root folder. const Globals = require.main.require(“./globals.js”); const YourCustomModule = require(“./yourmodule.js”); console.log(YourCustomModule.message); // — yourmodule.js — module.exports = { message: “Hello World!”, otherData: “Hello Grepper!” }; Here is what the above code is Doing: 1. The `require.main.require` function is used to get a module from the root…

delete row javascript

delete row javascript

function deleteThisRow() { this.closest(‘tr’).remove() } Here is what the above code is Doing: 1. We’re creating a function called deleteThisRow. 2. We’re using the this keyword to refer to the button that was clicked. 3. We’re using the closest method to find the closest ancestor that matches the given selector. In this case, we want…

js new line regex

js new line regex

/[\n\r]/g Here is what the above code is Doing: 1. We’re using the global variable ‘regex’ to store a regular expression. 2. The regular expression is looking for any newline characters (\n) or carriage returns (\r). 3. The ‘g’ at the end of the regular expression means that we want to search for all matches,…

how to trigger events when the document loads in js

how to trigger events when the document loads in js

document.addEventListener(“DOMContentLoaded”, ready); function ready() { alert(‘DOM is ready’); // image is not yet loaded (unless it was cached), so the size is 0x0 alert(`Image size: ${img.offsetWidth}x${img.offsetHeight}`); } Here is what the above code is Doing: 1. We create an element and set its src to the image URL. 2. We add an event listener to…

adonis andwhere

adonis andwhere

payments.where(‘amount’, ‘>’, 1000).andWhere(‘type’, ‘CASH’) Here is what the above code is Doing: 1. We’re using the where() method to filter the payments by amount. 2. We’re using the andWhere() method to filter the payments by type. 3. We’re using the get() method to get the results.

perform a function on each element of array javascript

perform a function on each element of array javascript

var new_array = old_array.map(function(e) { e.data = e.data.split(‘,’); return e; }); Here is what the above code is Doing: 1. We’re using the map function to iterate over each element in the old_array. 2. We’re splitting the data property of each element into an array of strings. 3. We’re returning the element. The map function…