DEV Community

Atit Patel
Atit Patel

Posted on • Updated on • Originally published at Medium

22 Utility Functions To Ace Your JavaScript Coding Interview

JavaScript Coding Assessment Cheatsheet 2021

One type of JavaScript coding interview question you might encounter will involve you writing 1–2 lines of code for a given question. These questions are usually simple enough to answer in 5 minutes, but sometimes we struggle with them due to the pressure of the interview. These functions will help to prepare for your JavaScript Interviews in 2021.

To reduce the pressure at times let’s get prepared ahead of time!

Photo by Patrick Perkins on Unsplash

1. Remove Duplicates from an Array

  • Arrays: these are some handy methods that we can use to remove duplicates from an array.
  1. Using lodash
let array = [2, 1, 2, 5, 6, 7, 8, 9, 9, 10];
let arrayuniq = .uniq(array);//[2, 1, 5, 6, 7, 8, 9, 10]

2. Using the filter

let array = [2, 1, 2, 5, 6, 7, 8, 9, 9, 10];
let list = array.filter((x, i, a) => a.indexOf(x) == i);
//[2, 1, 5, 6, 7, 8, 9, 10]

3. Using Set

let array = [2, 1, 2, 5, 6, 7, 8, 9, 9, 10];
let setuniq = [...new Set(array)];
//[2, 1, 5, 6, 7, 8, 9, 10]

2. Remove Duplicates from Array of Objects

  • Arrays of Objects: these are some handy methods that we can use to remove duplicates from an array of objects.
  1. Using lodash
let users = [
{ id: 1, name: "ted" },
{ id: 1, name: "bob" },
{ id: 3, name: "sara" },
{ id: 4, name: "test" },
{ id: 4, name: "test" },
{ id: 5, name: "abc" }
];
let uniqueUsersByID = _.uniqBy(users, "id");
//[{"id":1,"name":"ted"},{"id":3,"name":"sara"},{"id":4,"name":"test"},{"id":5,"name":"abc"}]

We can check unique data with multiple properties with this code.

const uniquewithMultipleProperties = _.uniqWith(
users,
(a, b) => a.id === b.id || a.name === b.name
);
//[{"id":1,"name":"ted"},{"id":3,"name":"sara"},{"id":4,"name":"test"},{"id":5,"name":"abc"}]

2. Using a filter

let filteruniquebyID = users.filter(
(v, i, a) => a.findIndex(t => t.id === v.id) === i
);
//[{"id":1,"name":"ted"},{"id":3,"name":"sara"},{"id":4,"name":"test"},{"id":5,"name":"abc"}]

We can check unique data with multiple properties with this code.

let filteruniquebyIDName = users.filter(
(v, i, a) => a.findIndex(t => t.id === v.id || t.name === v.name) === i
);
//[{"id":1,"name":"ted"},{"id":3,"name":"sara"},{"id":4,"name":"test"},{"id":5,"name":"abc"}]

3. Using Set

var set1 = Array.from(
users.reduce((m, t) => m.set(t.id, t), new Map()).values()
);
//[{"id":1,"name":"bob"},{"id":3,"name":"sara"},{"id":4,"name":"test"},{"id":5,"name":"abc"}]

You can check stackblitz here.

https://stackblitz.com/edit/remove-duplicates-arrayofobjects

3. Find an item in Array

  • below are some methods to find an item in the array
  1. includes: this method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
console.log(array.includes(2)); // returns true

2. every: this method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

let testevery1 = array.every(val=> val>3); //false

3. some: this method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

let testsome1 = array.some(val=> val>3); //true

4. lodash includes: checks if value is in collection .Returns true if value is found, else false.

let lodashtest9 =.includes(array, 1); // true
let lodashtest10 =.includes(array, 3, 2); // false

5. findIndex: this method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

let  testindex = array.findIndex(val => val > 1);
//0

6. find: this method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined are returned.

let testfind = array.find(el => (el > 2));
//5

7. filter: this method creates a new array with all elements that pass the test implemented by the provided function.

let testfilter1 = array.filter(val=> val>3);
//[5, 6, 7, 8, 9, 9, 10]

8. map: this method creates a new array populated with the results of calling a provided function on every element in the calling array.

let val = [];
array.map(item => { if(item >= 3) val.push(item); });
//[5, 6, 7, 8, 9, 9, 10]

You can check stackblitz here.

https://stackblitz.com/edit/find-item-array

4. Find an item in the Array of Objects

  • these are the methods that can be used to find an item in the array of objects.

1. every: this method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

let testevery2 = users.every(val=> val.id>3);
//false

2. some: this method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

let testsome2 = users.some(val=> val.id>3); //true

3. lodash includes: checks if value is in collection .Returns true if value is found, else false.

let lodashtest11 =.includes({ 'a': 1, 'b': 2 }, 1);
//true
let lodashtest12 =_.includes('abcd', 'bc');
//true

4. findIndex: this method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

let  testindex2 = users.findIndex(val => val.id > 1);
//3

5. find: this method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined are returned.

let testfind2 = users.find(el => (el.id > 2));
//{"id":3,"name":"sara"}

6. filter: this method creates a new array with all elements that pass the test implemented by the provided function.

let testfilter2 = users.filter(val=> val.id>3);

7. map: this method creates a new array populated with the results of calling a provided function on every element in the calling array.

let val2 = [];
users.map(item => { if(item.id >= 3) val2.push(item); });

You can check stackblitz here.

https://stackblitz.com/edit/find-item-array

5. Sort Array items

Arrays can be sort using the sort method.

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]

Continue reading on Medium »

Top comments (1)

Collapse
 
hey_yogini profile image
Yogini Bende

Why continue reading on Medium?