Skip to content

AmnaAbd/javascript-tricks

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 

Repository files navigation

javascript-tricks

javascript tricks

Arrays

Union of arrays

let a = [1, 2, 3, 4, 5];
let b = [3, 4, 5, 6, 7];

let result = [...new Set([...a, ...b])]; // [1, 2, 3, 4, 5, 6, 7]

Intersection of arrays

let a = [1, 2, 3, 4, 5];
let b = [2, 3, 4, 6];

let result = a.filter(val => ~b.indexOf(val)); // [2, 3, 4]

Get non intersected values

let a = [1, 2, 3, 4, 5];
let b = [2, 3, 4, 6];

let result = a.filter(val => !~b.indexOf(val)); // [1, 5]

Printing array of array values in a new line

const a = [
  [11],
  [7, 15],
  [5, 9, 13, 20],
  [3, 6, 8, 10, 12, 14, 18, 25]
];
const result = a.reduce((el, acc) => el + acc.join(' ') + '\n', '\n');

// output
/*
  11
  7 15
  5 9 13 20
  3 6 8 10 12 14 18 25
*/

Strings

Reverse a string

let str = str.split('').reverse().join('');

Replace a character at a particular index

function replaceAt(str, i, char) {
  return s.substring(0, i) + char + str.substring(i + 1);
}

Remove last character in a string

// using substring
str.substring(0, str.length - 1);

// using slice
str.slice(0, -1)

About

javascript tricks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published