Categories
JavaScript Answers

How to loop through arrays of arrays with JavaScript?

Sometimes, we want to loop through arrays of arrays with JavaScript.

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

How to loop through arrays of arrays with JavaScript?

To loop through arrays of arrays with JavaScript, we can use the for-of loop.

For instance, we write:

const printArray = (arr) => {
  for (const a of arr) {
    if (Array.isArray(a)) {
      printArray(a);
    } else {
      console.log(a);
    }
  }
}

const parentArray = [
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  [
    [10, 11, 12],
    [13, 14, 15],
    [16, 17, 18]
  ],
  [
    [19, 20, 21],
    [22, 23, 24],
    [26, 27, 28]
  ]
];
printArray(parentArray)

to define the printArray function that takes the arr array.

In it, we use the for-of loop to loop through each entry of arr.

And in the loop body, we check if a is an array with Array.isArray.

If it’s an array, we call printArray again.

Otherwise, we log the value with console.log.

Therefore, when we call printArray with parentArray, we see all the values logged.

Conclusion

To loop through arrays of arrays with JavaScript, we can use the for-of loop.

Categories
JavaScript Answers

How to get text between double quotes with JavaScript?

Sometimes, we want to get text between double quotes with JavaScript.

In this article, we’ll look at how to get text between double quotes with JavaScript.

How to get text between double quotes with JavaScript?

To get text between double quotes with JavaScript, we can use a regex.

For instance, we write:

const s = `"hello world"`
const [_, text] = s.match(/"((?:\\.|[^"\\])*)"/)
console.log(text)

to call s.match with /"((?:\\.|[^"\\])*)"/ to return an object with the text between the double quotes in the 2nd entry.

Therefore, text is 'hello world'.

Conclusion

To get text between double quotes with JavaScript, we can use a regex.

Categories
JavaScript Answers

How to increment value each time when you run function with JavaScript?

Sometimes, we want to increment value each time when you run function with JavaScript.

In this article, we’ll look at how to increment value each time when you run function with JavaScript.

How to increment value each time when you run function with JavaScript?

To increment value each time when you run function with JavaScript, we can create a variable outside the function and increment that when the function is run.

For instance, we write:

let count = 0
const f = () => {
  count++
}
f()
f()
console.log(count)

to define the count variable.

Then we define function f that increments count by 1.

Then we call f twice.

As a result, count is 2 according to the console log.

Conclusion

To increment value each time when you run function with JavaScript, we can create a variable outside the function and increment that when the function is run.

Categories
JavaScript Answers

How to remove the first character of a class name with JavaScript?

Sometimes, we want to remove the first character of a class name with JavaScript.

In this article, we’ll look at how to remove the first character of a class name with JavaScript.

How to remove the first character of a class name with JavaScript

To remove the first character of a class name with JavaScript, we can use the string’s substring method.

For instance, we write:

<div class='foo'>

</div>

to add a div with a class attribute set.

Then we write:

const div = document.querySelector('div')
const trimmed = div.className.substring(1);
console.log(trimmed)

to select the div with querySelector.

Then we get the class name with className.

And then we call substring with 1 to return the class name string without the first character.

Therefore trimmed is 'oo'.

Conclusion

To remove the first character of a class name with JavaScript, we can use the string’s substring method.

Categories
JavaScript Answers

How to generate short unique alphanumeric with Node.js and JavaScript?

Sometimes, we want to generate short unique alphanumeric with Node.js and JavaScript.

In this article, we’ll look at how to generate short unique alphanumeric with Node.js and JavaScript.

How to generate short unique alphanumeric with Node.js and JavaScript?

To generate short unique alphanumeric with Node.js and JavaScript, we can use the number’s toString method with base 36.

For instance, we write:

const s = (100000000000).toString(36);
console.log(s)

to call toString on 100000000000 with 36 to return a string with letters and digits.

As a result, s is '19xtf1ts'.

Conclusion

To generate short unique alphanumeric with Node.js and JavaScript, we can use the number’s toString method with base 36.