Skip to content

bimbowande/Javascript-tricks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

Javascript-tricks

Learning basic tricks in Javascript.

Arrays

Manipulating Arrays

  1. Removing duplicates from an array
/*
 #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' ]
  
  /*
  # method 3
  */
  
  uniqueArray = [];
 fruits.forEach((value) => !uniqueArray.includes(value) ? uniqueArray.push(value) : uniqueArray )
  1. Replace the specific value in an array
    var fruits2 = [...fruits];
    
    fruits2.splice(0,2,"potato","tomato");
    
    // fruits2  ['potato', 'tomato', 'apple', 'sugarcane', 'manago', 'apple' ]
  1. Mapping array without maps
  
  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' ]

  1. Emptying an array
  let fruits = ['banana','mango','apple','sugarcane','mango','apple']
  
  fruits.length = 0;
  
  // fruits -- []
  1. Convert an array to an object
   let fruits = ['banana','manago','apple','sugarcane','manago','apple']
   
   const fruitsObj = {...fruits};
   
   //  fruitsObj --  {'0': 'banana','1': 'mango', '2': 'apple','3': 'sugarcane','4': 'mango','5': 'apple'}
 
  1. Fulfill array with data
  let newArray = new Array(10).fill("1");
  
  // newArray -- ['1', '1', '1', '1','1', '1', '1', '1','1', '1']
  1. Merging Arrays
  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']
  1. Merging only duplicates value in two arrays
    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 ]
  1. Remove falsy value from an array.
    const mixedArray = [0,"blue","",NaN,true,undefined,"white",false];
    const filteredArray = mixedArray.filter(Boolean);
    
    // filteredArray -- [ 'blue', true, 'white' ]
  1. Reverse an array
  const colors = ['blue','red','green','black'];
  
  const reverseColors = colors.reverse();
  // reverseColors  -- [ 'black', 'green', 'red', 'blue' ]
  1. Sum of the value in an array
  const val = [1,4,5,7];
  
  const valSum = val.reduce((x,y)=>x+y);
  
  // valSum --  17
  1. Generate an array from a function Arguments
 function f() {
    return Array.from(arguments);
  }
  f(1,2,4))
  
  // result -- [1,2,4]
  1. Sorting the value in an array
 const sortNumber = (values) => {
    if(!Array.isArray(values)){
      return null
    }
    return  values.sort((a,b)=>a-b);
  }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published