Common React Coding Interview Questions
Coding Interview Questions
1. Remove Duplicates in below array of objects
const persons = [
{name: "rakesh", age: 25, salary: 10000, id: 1},
{name: "rakesh", age: 25, salary: 10000, id: 1},
{name: "mahesh", age: 37, salary: 100000, id: 2},
{name: "mahesh", age: 37, salary: 100000, id: 2},
{name: "mahesh", age: 37, salary: 100000, id: 2},
{name: "rakesh", age: 32, salary: 70000, id: 3},
{name: "rakesh", age: 32, salary: 70000, id: 3},
{name: "ramesh", age: 30, salary: 60000, id: 4},
{name: "ramesh", age: 30, salary: 60000, id: 4},
{name: "ramesh", age: 30, salary: 60000, id: 4},
{name: "sukesh", age: 29, salary: 40000, id: 5},
{name: "sukesh", age: 29, salary: 40000, id: 5},
{name: "sukesh", age: 29, salary: 40000, id: 5}
]
Solution:
const uniqueArr = [];
const seenIds = {};
for(let person of persons){
if(!seenIds[person.id]){
seenIds[person.id] = true;
uniqueArr.push(person)
}
}
console.log(uniqueArr)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. Remove duplicates in array of strings 1st way
Solution:
const alphabets = ["a", "b", "c", "a", "b", "c"]
const uniqueAlphabets = [...new Set(alphabets)]
console.log(uniqueAlphabets);
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. Remove Duplicates in array 2nd way
Solution:
const letters = ["a", "b", "c", "a", "b", "c"]
const uniqueArr = []
for(let letter of letters){
if(!uniqueArr.includes(letter)){
uniqueArr.push(letter)
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
4. Sort an array of strings
const fruits = ["mango", "grapes", "apples", "banana"]
const sortedArr = fruits.sort()
console.log(sortedArr)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
5. Sort in ascending & descending order
Solution:
const numbers = [1, 543, 65, 76, 43]
const ascendingSortedNumbers = numbers.sort((a, b) => a-b)
const descendingSortedNumbers = numbers.sort((a, b) => b-a)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
6. Sort by salary of persons in descending and ascending order for below array of objects
const arr = [{name: "rakesh", age: 25, salary: 10000}, {name: "mahesh", age: 28, salary: 100000}, {name: "ramesh", age: 29, salary: 70000}, {name: "mukesh", age: 27, salary: 60000}]
const sortByAsc = arr.sort((a,b) => a.salary - b.salary)
const sortByDsc = arr.sort((a,b) => b.salary - a.salary)
console.log(sortByAsc)
console.log(sortByDsc)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Conversion of String to Array and Array to String
let string = "rajesh"
const array = [...string]
console.log(array) // Output: ["r", "a", "j", "e", "s", "h"]
const newArray = ["r", "a", "m", "e", "s", "h"]
const str = newArray.join("")
console.log(str) // Output: "ramesh"
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Object Methods
const obj = {
name: "rajesh",
age: 28
}
Object.keys(obj) // Output: ["name", "age"]
Object.values(obj) // Output: ["rajesh", "28"]
Object.entries(obj) // Output: [["name", "rajesh"], ["age", "28"]]
Object.freeze(obj) // Used to avoid mutating the original object
Object.seal(obj) // Used to mutate the original object but not allowed additional properties
Object.isFrozen(obj) // Returns true/false if it is freezed/unfreezed
Object.isSealed(obj) // Returns true/false if it is sealed/unsealed
Object.hasOwn(obj, 'age') // Return true/false if the property exists
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ways to Deep clone of Array and Objects
const arr = [1, 2, 3, 4, 5]
const arr1 = [...arr]
const arr2 = JSON.parse(JSON.stringify(arr))
const arr3 = structuredClone(arr)
import cloneDeep from "lodash/cloneDeep";
const arr4 = cloneDeep(arr)
Comments
Post a Comment