Categories
JavaScript Answers

How to sort an array of objects by date with JavaScript?

To sort an array of objects by date with JavaScript, we call the sort method.

For instance, we write

const recent = [
  { id: 123, age: 12, start: "10/17/2022 13:07" },
  { id: 13, age: 62, start: "07/30/2022 16:30" },
];
const sorted = recent.sort((a, b) => {
  return new Date(a.start).getTime() - new Date(b.start).getTime();
});

to call recent.sort with a callback that gets the timestamp of the start property of 2 of the objects in recent with getTime and then compare them by subtracting them to return a number that determines the sorting order.

a is sorted after b if the number returned is bigger than 0.

a is sorted before b if the number returned is less than 0.

And the original order is kept if it’s 0.

Categories
JavaScript Answers

How to use getElementById with multiple IDs in JavaScript?

To use getElementById with multiple IDs in JavaScript, we use the querySelectorAll method.

For instance, we write

const els = document.querySelectorAll(
  "#myCircle1, #myCircle2, #myCircle3, #myCircle4"
);

to call querySelectorAll with the selector for the elements we want to select separated by commas.

A node list with all the selected elements is returned.

Categories
JavaScript Answers

How to push with a multidimensional array with JavaScript?

To push with a multidimensional array with JavaScript, we call the push method.

For instance, we write

arrayToPush.push([value1, value2, valueN]);

to call arrayToPush.push with an array of values to append the array as the last element of arrayToPush.

Categories
JavaScript Answers

How to filter array of objects whose any properties contain a value with JavaScript?

To filter array of objects whose any properties contain a value with JavaScript, we call the filter method.

For instance, we write

const filterByValue = (array, string) => {
  return array.filter((o) =>
    Object.values(o).some((v) => v.toLowerCase().includes(string.toLowerCase()))
  );
};

const arrayOfObject = [
  { name: "Paul", country: "Canada" },
  { name: "Lea", country: "Italy" },
  { name: "John", country: "Italy" },
];

console.log(filterByValue(arrayOfObject, "lea"));

to define the filterByValue function.

In it, we call array.filter with a callback that calls Object.values with o to return an array of o‘s property values.

Then we call some with the array to check if v includes string if they’re both converted to lower case with toLowerCase with includes.

Next, we call filterByValue with arrayOfObject and the value we’re checking for.

Categories
JavaScript Answers

How to check if a string contains any element of an array in JavaScript?

To check if a string contains any element of an array in JavaScript, we call the filter method.

For instance, we write

const arr = ["banana", "monkey banana", "apple", "kiwi", "orange"];

const checker = (value) =>
  !["banana", "apple"].some((element) => value.includes(element));

console.log(arr.filter(checker));

to call arr.filter with the checker function.

In checker, we check if there’re any strings that include 'banana' or 'apple' in value in the arr array with includes.

some returns true if its callback returns true.

So we negate it to find the values in arr that don’t include 'banana' or 'apple'.

filter returns an array with the strings that don’t include 'banana' or 'apple'.