DEV Community

Cover image for Javascript snippets you need to know right now πŸ”₯ - #3
Abhiraj Bhowmick
Abhiraj Bhowmick

Posted on • Originally published at abhiraj.mdx.one

Javascript snippets you need to know right now πŸ”₯ - #3

Hello friends!
Hope you all are doing great.
Welcome back to my series of posts where I give 10 JS snippets every week amounting over to 50 essential JS snippets.

Here's the previous edition if you missed it.

1️⃣ average
This snippet returns the average of two or more numerical values.

const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;
average(...[1, 2, 3]); // 2
average(1, 2, 3); // 2
Enter fullscreen mode Exit fullscreen mode

2️⃣ averageBy
This snippet returns the average of an array after initially doing the mapping of each element to a value using a given function.

const averageBy = (arr, fn) =>
  arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
  arr.length;

averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5
Enter fullscreen mode Exit fullscreen mode

3️⃣ capitalizeEveryWord
This snippet capitalizes the first letter of every word in a given string.

const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());

capitalizeEveryWord('hello world!'); // 'Hello World!'
Enter fullscreen mode Exit fullscreen mode

4️⃣ Create Directory
This snippet uses existsSync() to check whether a directory exists and then mkdirSync() to create it if it doesn’t.

const fs = require('fs');
const createDirIfNotExists = dir => (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined);
createDirIfNotExists('test'); 
// creates the directory 'test', if it doesn't exist
Enter fullscreen mode Exit fullscreen mode

5️⃣ deepFlatten
This snippet flattens an array recursively.

const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));

deepFlatten([1, [2], [[3], 4], 5]); // [1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

6️⃣ difference
This snippet finds the difference between two arrays.

const difference = (a, b) => {
  const s = new Set(b);
  return a.filter(x => !s.has(x));
};

difference([1, 2, 3], [1, 2, 4]); // [3]
Enter fullscreen mode Exit fullscreen mode

7️⃣ differenceBy
This method returns the difference between two arrays, after applying a given function to each element of both lists.

const differenceBy = (a, b, fn) => {
  const s = new Set(b.map(fn));
  return a.filter(x => !s.has(fn(x)));
};

differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1.2]
differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [ { x: 2 } ]
Enter fullscreen mode Exit fullscreen mode

8️⃣ differenceWith
This snippet removes the values for which the comparator function returns false.

const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);

differenceWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0], (a, b) => Math.round(a) === Math.round(b)); 
// [1, 1.2]
Enter fullscreen mode Exit fullscreen mode

9️⃣ digitize
This snippet gets a number as input and returns an array of its digits.

const digitize = n => [...`${n}`].map(i => parseInt(i));

digitize(431); // [4, 3, 1]
Enter fullscreen mode Exit fullscreen mode

πŸ”Ÿ distance
This snippet returns the distance between two points by calculating the Euclidean distance.

const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);

distance(1, 1, 2, 3); // 2.23606797749979
Enter fullscreen mode Exit fullscreen mode

Thank you for reading. Hope this has been of some help to you.
Subscribe to my newsletter to never miss out on such posts and many other tech news and product launches.

Abhiraj's Dev-letter

Until next time,
Abhiraj

Top comments (3)

 
abhirajb profile image
Abhiraj Bhowmick

Agreed.

Collapse
 
abhirajb profile image
Abhiraj Bhowmick

Great! Thanks for adding on. Have a nice one!

Collapse
 
abhirajb profile image
Abhiraj Bhowmick

Absolutely, one should learn JS and then turn to these snippets for better understanding and quick reference.