avatarKirti K

Summary

The website content provides a comprehensive guide for interview preparation on JavaScript basics, including methods for sorting arrays of objects based on specific criteria.

Abstract

The article titled "JavaScript Day 42 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation" is part of a series aimed at beginners preparing for JavaScript interviews. It covers practical examples of sorting arrays of objects, such as sorting cars by age or by a dynamic key like 'make', 'model', or 'year'. The guide offers two solutions for each sorting challenge: a concise, modern JavaScript approach using the sort function with arrow functions and the spread operator, and a more traditional approach using a custom compare function. The article emphasizes the importance of understanding JavaScript basics and encourages sharing knowledge, being kind, and respecting others in the learning process. It also invites readers to subscribe and follow the author's blog for further support and learning resources.

Opinions

  • The author values the importance of mastering JavaScript basics for interview preparation.
  • Sharing knowledge is seen as a means to gain knowledge, with the author encouraging readers to engage in this exchange.
  • The author suggests that being kind and treating people with respect is crucial in the tech community.
  • The guide is tailored for beginners, indicating the author's commitment to supporting newcomers in the field.
  • The inclusion of multiple sorting methods demonstrates the author's belief in providing comprehensive solutions to problems.
  • By inviting readers to subscribe and follow, the author seeks to build a community of learners and establish a supportive network.

JavaScript Day 42 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

JavaScript Day 42 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

Question 1 — How to sort an array of objects based on a car age ?

const cars = [
  { name: 'i10', age: 10 },
  { name: 'magna', age: 25 },
  { name: 'ford', age: 35 },
];

Answer -

First Solution

[...cars].sort((a,b)=>{
     return a.age - b.age ;
})

console.log(cars)

Second Solution

function compareAge(a, b) {
  return a.age - b.age;
}

// Sort the array based on age
cars.sort(compareAge);

console.log(cars);

Question 2 — How to sort an array of objects based to dynamic key?

const cars = [
  { make: 'Toyota', model: 'Corolla', year: 2018 },
  { make: 'Honda', model: 'Civic', year: 2019 },
  { make: 'Ford', model: 'Focus', year: 2017 },
];

Answer

Step 1 — Create the custom function

Step 2 — Access the dynamic key with array.sort

a[key] and b[key] access the values of the objects based on the dynamic key

Step 3 — Use the compare condition and return

Step 4 — Use sortByKey method

sortByKey function takes an array of objects and a dynamic key as parameters.

// Function to sort array of objects based on dynamic key
function sortByKey(array, key) {
  return array.sort((a, b) => {
    // Access the values of the objects using the dynamic key
    const valueA = a[key];
    const valueB = b[key];
  
    // Compare the values
    if (valueA < valueB) return -1;
    if (valueA > valueB) return 1;
    return 0;
  });
}

// Sort cars array based on the 'year' key
const sortedCarsByModel = sortByKey(cars, 'model');
console.log(sortedCarsByModel);

Happy Coding and Stay Tuned :)

Kind is always cool !!!

Share knowledge to gain knowledge !!!!

Be kind and treat people with respect !!

If you find my blog helpful, then please subscribe, claps and follow to support :)

Thank you -JavaScript Day 42 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
JavaScript
Javascript Interview
Frontend Interviews
Coding Interviews
Programming
Recommended from ReadMedium