Categories
JavaScript Answers

How to Sort Arrays in JavaScript by Object Key Value?

Sometimes, we want to sort arrays in JavaScript by object key value.

In this article, we’ll look at how to sort arrays in JavaScript by object key value.

Sort Arrays in JavaScript by Object Key Value

To sort arrays in JavaScript by object key value, we can use the JavaScript array’s sort method.

For instance, we can write:

const arr = [{
  name: "alex"
}, {
  name: "clex"
}, {
  name: "blex"
}];
const sorted = arr.sort((a, b) => (a.name > b.name ? 1 : -1))
console.log(sorted)

to sort the arr array by the value of the name properties.

We call arr.sort with a callback that compare the name properties of entries a and b in the arr object.

If a.name is bigger than b.name , we return 1.

Otherwise, we return -1.

The comparison operator will compare 2 string’s by their lexical order.

Therefore, sorted is:

[
  {
    "name": "alex"
  },
  {
    "name": "blex"
  },
  {
    "name": "clex"
  }
]

Conclusion

To sort arrays in JavaScript by object key value, we can use the JavaScript array’s sort method.

Categories
JavaScript Answers

How to Split a String Once in JavaScript?

Sometimes, we want to split a string once in our JavaScript code.

In this article, we’ll look at how to split a string once in our JavaScript code.

Split a String Once in JavaScript

To split a string once in our JavaScript code, we can use the JavaScript string’s indexOf method with the JavaScript string’s slice method to find the index of the first instance of the separator and then do the slicing.

For instance, we can write:

const s = "1|Ceci n'est pas une pipe: | Oui";
const i = s.indexOf('|');
const splits = [s.slice(0, i), s.slice(i + 1)];
console.log(splits)

We have the s string that we want to split by the first | .

Then we call indexOf with '|' to get the index of the first instance of the | symbol.

Next, we call slice with 0 and i to get the substring before the first | .

And likewise, we call slice with i + 1 to get the part after the first | .

We then put both substrings into the splits array.

Therefore, splits is:

["1", "Ceci n'est pas une pipe: | Oui"]

Conclusion

To split a string once in our JavaScript code, we can use the JavaScript string’s indexOf method with the JavaScript string’s slice method to find the index of the first instance of the separator and then do the slicing.

Categories
JavaScript Answers

How to Extend a JavaScript Class?

Sometimes, we want to extend a JavaScript class.

In this article, we’ll look at how to extend a JavaScript class.

Extend a JavaScript Class

To extend a JavaScript class, we can use the extends keyword and the class syntax.

For instance, we can write:

class Person {
  sayHello() {
    console.log('hello');
  }

  walk() {
    console.log('I am walking!');
  }
}

class Student extends Person {
  sayGoodBye() {
    console.log('goodbye');
  }

  sayHello() {
    console.log('Hi, I am a student');
  }
}

const student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();

console.log(student1 instanceof Person);
console.log(student1 instanceof Student);

We have the Person class that serves as the base class for the Student class.

It has the sayHello and walk methods.

Then we have the Student class which inherits the contents of the Person class.

It also has its own methods, which are sayGoodBye and sayHello .

The extends keywords indicates that Student has Person as its base class, so it should also inherit methods from the Person class.

Next, we create a new Student instance with the new keyword.

And we call all the methods from the Student class itself and the Person class from student1 .

Also, both console logs should log true because student1 is a Student instance and Student ‘s base class is Person .

Conclusion

To extend a JavaScript class, we can use the extends keyword and the class syntax.

Categories
JavaScript Answers

How to Check if the Inputted Value is a Number or Letter with JavaScript?

Sometimes, we want to check if the inputted value is a number or letter with JavaScript.

In this article, we’ll look at how to check if the inputted value is a number or letter with JavaScript.

Check if the Inputted Value is a Number or Letter with JavaScript

To check if the inputted value is a number or letter with JavaScript, we can get the inputted value of the input element with the value property.

Then we can use the isNaN function to check if the inputted value is a number or not.

For instance, if we have the following form:

<form name="myForm">
  Age: <input type="text" name="age">
  <input type="submit" value="Submit">
</form>

Then we can get the form and check that the age field has a number entered in it by writing:

const {
  myForm
} = document.forms

myForm.addEventListener('submit', (e) => {
  e.preventDefault()
  const x = myForm.age.value;
  if (isNaN(x)) {
    alert("Must input numbers");
  }
})

We get the form by its name attribute value from document.forms .

Then we call addEventListener with 'submit' to add a submit event listener to the form.

The 2nd argument is the submit event handler callback that calls e.preventDefault to prevent default server-side submission.

Then it gets the value of from the myForm.age field, which is the input with name attribute set to age .

Next, we check if the x string is not a number with isNaN .

If it returns true , then we know it’s not a number string, and we display an alert to the user with alert.

Otherwise, it’s a number.

Conclusion

To check if the inputted value is a number or letter with JavaScript, we can get the inputted value of the input element with the value property.

Then we can use the isNaN function to check if the inputted value is a number or not.

Categories
JavaScript Answers

How to Get the Width of an Element in Pixels that has the Style CSS Property Set with % with JavaScript?

Sometimes, we want to get the width of an element in pixels that has the style CSS property set with % with JavaScript.

In this article, we’ll look at how to get the width of an element in pixels that has the style CSS property set with % with JavaScript.

Get the Width of an Element in Pixels that has the Style CSS Property Set with % with JavaScript

To get the width of an element in pixels that has the style CSS property set ith % with JavaScript, we can use the clientWidth property of the element.

For instance, if we have the following HTML:

<div style="width: 100%; height: 10px;"></div>

with the width set to 100% and we want to get the width in pixels.

Then we can get the width of the div in pixels by writing:

const {  
  clientWidth  
} = document.querySelector('div')  
console.log(clientWidth)

We select the div with document.querySelector .

And then we destructure the clientWidth property from the selected element object.

clientWidth is the width of the div in pixels.

Conclusion

To get the width of an element in pixels that has the style CSS property set ith % with JavaScript, we can use the clientWidth property of the element.