Categories
JavaScript Answers

How to loop through childNodes with JavaScript?

Sometimes, we want to loop through childNodes with JavaScript.

In this article, we’ll look at how to loop through childNodes with JavaScript.

How to loop through childNodes with JavaScript?

To loop through childNodes with JavaScript, we can use a for-of loop.

For instance, we write

const children = element.childNodes;

for (const child of children) {
  console.log(child);
}

to get the child nodes of the element with the childNodes property.

Then we loop through the children child nodes with a for-of loop.

We get the node being looped through from child.

Conclusion

To loop through childNodes with JavaScript, we can use a for-of loop.

Categories
JavaScript Answers

How to calculate the date that is 2 days ago with JavaScript?

Sometimes, we want to calculate the date that is 2 days ago with JavaScript.

In this article, we’ll look at how to calculate the date that is 2 days ago with JavaScript.

How to calculate the date that is 2 days ago with JavaScript?

To calculate the date that is 2 days ago with JavaScript, we use the getDate and setDate methods.

For instance, we write

const d = new Date();
d.setDate(d.getDate() - 2);
console.log(d.toString());

to create the date d with the current date and time.

Then we get the date of the current date with getDate and subtract 2 from it to get the date 2 days ago.

And then we use the difference as the argument for setDate.

Finally, we call toString to return a date string version of d.

Conclusion

To calculate the date that is 2 days ago with JavaScript, we use the getDate and setDate methods.

Categories
JavaScript Answers

How to use variable in string match with JavaScript?

Sometimes, we want to use variable in string match with JavaScript.

In this article, we’ll look at how to use variable in string match with JavaScript.

How to use variable in string match with JavaScript?

To use variable in string match with JavaScript, we can use the RegExp constructor to create our regex.

For instance, we write

const word = "Web Development Tutorial";
const vowels = "[aeiou]";
const re = new RegExp(vowels, "gi");
const arr = word.match(re);

to create a regex that match vowels with

const re = new RegExp(vowels, "gi");

The first argument is the regex pattern string and the 2nd argument is the string with the flags.

g means we return all matches and i means we search in a case-insensitive manner.

Then we call word.match with re to return an array of matches.

Conclusion

To use variable in string match with JavaScript, we can use the RegExp constructor to create our regex.

Categories
JavaScript Answers

How to sort objects by property values with JavaScript?

Sometimes, we want to sort objects by property values with JavaScript.

In this article, we’ll look at how to sort objects by property values with JavaScript.

How to sort objects by property values with JavaScript?

To sort objects by property values with JavaScript, we can use the array sort method.

For instance, we write

const cars = [
  {
    name: "Honda",
    speed: 80,
  },

  {
    name: "BMW",
    speed: 180,
  },

  {
    name: "Trabi",
    speed: 40,
  },

  {
    name: "Ferrari",
    speed: 200,
  },
];

const sortedCars = cars.sort((a, b) => {
  return a.speed - b.speed;
});

to call cars.sort with a function that returns a.speed - b.speed to sort the cars entries by the speed property value of each entry in ascending order.

Conclusion

To sort objects by property values with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to insert an array inside another array with JavaScript?

Sometimes, we want to insert an array inside another array with JavaScript.

In this article, we’ll look at how to insert an array inside another array with JavaScript.

How to insert an array inside another array with JavaScript?

To insert an array inside another array with JavaScript, we can use the array splice method.

For instance, we write

a1.splice(2, 0, ...a2);

to call a1.splice with 2, 0 and the entries in array a2 as arguments.

We insert the elements of a2 from index 2 of a1 and we delete 0 entries from a1 before we insert them.

Conclusion

To insert an array inside another array with JavaScript, we can use the array splice method.