Make daily JS array operations easy with these one liners

Photo by Joan Gamell on Unsplash

Make daily JS array operations easy with these one liners

Advance plays with JS arrays

There are multiple ways one can do a task in JavaScript. Over time, JS introduced many new methods. Some provided more readability, some reduced lines of code and some with improved performance.

In the below article, I'll cover some methods on JS array which you might find frequent use in daily coding.

  1. Remove duplicates from an array

    There are multiple ways we could achieve this. Previous to the introduction of Set the usual ways of handling this involved nested array methods thus amounting to higher time complexity. Set() makes it easy.

     const arr = [1, 2, 3, 4, 5, 3, 6, 2, 7, 6];
     const arrWithoutDuplicates = [...new Set(arr)];
    
  2. Check if an element with your condition exists

    There are multiple ways to do it including the usual for..loop with a flag or find()/filter() . But we have a better method some()

     const employees = [{
         id: 1,
         name: 'John Doe',
         dept: 'IT',
     }, {
         id: 2,
         name: 'Jane Doe',
         dept: 'Admin'
     }, {
         id: 3,
         name: 'Test Doe',
         dept: 'Admin'
     }];
    
     // To find if employee with Dept IT exists
     return employees.some(emp => emp.dept === 'IT');
    
  3. Check if every element which satisfies your condition exists

     return employees.every(emp => emp.dept === 'IT');
    
  4. Get the largest(and smallest) element from the array

    .reduce() finds many interesting usages in comparison and conversions in an array.

     const largest = (arr) =>
       arr.reduce((largest, num) => Math.max(largest, num));
     const arr = [10, 16, 24, 12, 8, 10, 21];
    
     console.log(largest(arr)); // 24
    
  5. Convert an array of objects to an Object

    Finding an element in an array is always costlier than finding it in an object. Objects in JS are like hashes or hash tables.

    Array to Object conversion gives bigger gains in comparisons, basically reducing time complexity from O(n²) to ~O(n)

     // Consider you have to merge these arrays
     const employeeSalaries = [{ id: 1, salary: 100000}, {id: 2, salary: 150000}];
     const employees = [{
         id: 1,
         name: 'John Doe',
         dept: 'IT',
     }, {
         id: 2,
         name: 'Jane Doe',
         dept: 'Admin'
     }, {
         id: 3,
         name: 'Test Doe',
         dept: 'Admin'
     }];
    
     const empSals = employeeSalaries.reduce((acc, curr) => {
         acc[curr.id] = curr.salary; // or acc[curr.id] = true, if you do not need reference to original object
         return acc;
     }, {});
     // { '1': 100000, '2': 150000...}
    
     const mergedArray = employees.map((e) => ({...e, salary: empSals[e.id]});
    

That's all folks!

JavaScript is funny, and at the same time complex. Such small snippets would definitely help you in writing code, cracking interviews and understanding concepts.