javascript-tricks
Published: 2022-01-11 12:58:27

## Arrays

### Union of arrays
```javascript
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
```javascript
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
```javascript
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
```javascript
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
*/
```
### Removing duplicates from an array

```Javascript

/*
 #method 1
*/

  let fruits = ['banana','mango','apple','sugarcane','mango','apple']

  let uniqueFruits = Array.from(new Set(fruits));

 // uniqueFruits -- [ 'banana', 'manago', 'apple', 'sugarcane' ]

/*
 # method 2
*/

  let uniqueFruits2 = [...new Set(fruits)];
  
  // uniqueFruits2 -- [ 'banana', 'manago', 'apple', 'sugarcane' ]

```

### Replace the specific value in an array

```Javascript
    var fruits2 = [...fruits];
    
    fruits2.splice(0,2,"potato","tomato");
    
    // fruits2  ['potato', 'tomato', 'apple', 'sugarcane', 'manago', 'apple' ]

```
### Mapping array without maps

```JavaScript
  
  let friends = [
      {name:"John", age:22},
      {name:"Peter", age:23},
      {name:"bimbo",age:34},
      {name:"mark",age:45},
      {name:"Esther",age:21},
      {name:"Monica",age:19}
  ];
  
  let friendNames = Array.from(friends,({name})=>name);
  
  // friendNames --   ['John', 'Peter', 'bimbo', 'mark', 'Esther', 'Monica' ]



```
### Emptying an array

```JavaScript
  let fruits = ['banana','mango','apple','sugarcane','mango','apple']
  
  fruits.length = 0;
  
  // fruits -- []
```
### Convert an array to an object

``` JavaScript
   let fruits = ['banana','manago','apple','sugarcane','manago','apple']
   
   const fruitsObj = {...fruits};
   
   //  fruitsObj --  {'0': 'banana','1': 'mango', '2': 'apple','3': 'sugarcane','4': 'mango','5': 'apple'}
 
 ```
 ### Fulfill array with data

```JavaScript
  let newArray = new Array(10).fill("1");
  
  // newArray -- ['1', '1', '1', '1','1', '1', '1', '1','1', '1']

```
### Merging Arrays

```JavaScript
  let fruits = ['banana','manago','apple'];
  let meat = ["Poultry","beef","fish"]
  let vegetables = ["Potato","tomato","cucumber"];
  let food = [...fruits,...meat,...vegetables];
  
  // food  -- ['banana',  'manago','apple',   'sugarcane','manago',  'apple','Poultry', 'beef','fish',    'Potato','tomato',  'cucumber']

```
### Merging only duplicates value in two arrays

```JavaScript
    let numOne = [0,2,4,6,8,8];
    let numTwo = [1,2,3,4,5,6];

    const duplicatedValue = [...new Set(numOne)].filter(item=>numTwo.includes(item));
    // duplicatedValue -- [ 2, 4, 6 ]

```
### Remove falsy value from an array.

```JavaScript
    const mixedArray = [0,"blue","",NaN,true,undefined,"white",false];
    const filteredArray = mixedArray.filter(Boolean);
    
    // filteredArray -- [ 'blue', true, 'white' ]

```
### Reverse an array

```JavaScript
  const colors = ['blue','red','green','black'];
  
  const reverseColors = colors.reverse();
  // reverseColors  -- [ 'black', 'green', 'red', 'blue' ]

```
### Sum of the value in an array

``` JavaScript
  const val = [1,4,5,7];
  
  const valSum = val.reduce((x,y)=>x+y);
  
  // valSum --  17
```
### Generate an array from a function Arguments

``` JavaScript
 function f() {
    return Array.from(arguments);
  }
  f(1,2,4))
  
  // result -- [1,2,4]

```

## Strings

### Reverse a string

```javascript
let str = str.split('').reverse().join('');
```
### Replace a character at a particular index

```javascript
function replaceAt(str, i, char) {
  return s.substring(0, i) + char + str.substring(i + 1);
}
```
### Remove last character in a string
```javascript

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

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