Categories
JavaScript Answers

How to search an array of arrays with JavaScript?

Sometimes, we want to search an array of arrays with JavaScript.

In this article, we’ll look at how to search an array of arrays with JavaScript.

How to search an array of arrays with JavaScript?

To search an array of arrays with JavaScript, we can convert the nested arrays to a JSON string and them compare them.

For instance, we write:

const arr = [
  [2, 6, 89, 45],
  [3, 566, 23, 79],
  [434, 677, 9, 23]
];

const arrToFind = [3, 566, 23, 79]
const result = arr
  .map(JSON.stringify)
  .findIndex(a => {
    return a === JSON.stringify(arrToFind)
  })
console.log(result)

to find arrToFind in arr.

To do this, we call arr.map with JSON.stringify to convert each nested array to JSON strings.

Then we call findIndex with a callback that checks if a is the same as the stringified version of arrToFind.

Therefore, we see that result is 1 from the log.

Conclusion

To search an array of arrays with JavaScript, we can convert the nested arrays to a JSON string and them compare them.

Categories
JavaScript Answers

How to change color of specific words in text area with JavaScript?

Sometimes, we want to change color of specific words in text area with JavaScript.

In this article, we’ll look at how to change color of specific words in text area with JavaScript.

How to change color of specific words in text area with JavaScript?

To change color of specific words in text area with JavaScript, we can use a contenteditable div as the text area.

Then we can replace the matched substrings to the HTML code we want and assign it to the innerHTML property of the contentedtable div.

For instance, we write:

<div contenteditable="true" style='height: 200px'></div>

to add a contenteditable div.

Then we write:

const div = document.querySelector('div')
div.onblur = (e) => {
  e.target.innerHTML = e.target.innerHTML.replace(/select/g, (match) => {
    return `<span style='color: orange'>${match}</span>`
  })
}

to select the div with querySelector.

Then we set the div.onblur method to a function that calls e.target.innerHTML.replace to select all the substrings with the word 'select'.

Then we pass in a callback that does the replacement on all of them by replacing them with a span that sets their color to orange.

Now when we focus away from the div, we should see the color of ‘select’ change to orange.

Conclusion

To change color of specific words in text area with JavaScript, we can use a contenteditable div as the text area.

Then we can replace the matched substrings to the HTML code we want and assign it to the innerHTML property of the contentedtable div.

Categories
JavaScript Answers

How to sort a date string array with JavaScript?

Sometimes, we want to sort a date string array with JavaScript.

In this article, we’ll look at how to sort a date string array with JavaScript.

How to sort a date string array with JavaScript?

To sort a date string array with JavaScript, we can use the JavaScript date’s sort method.

For instance, we write:

const data = ["09/06/2015", "25/06/2015", "22/06/2015", "25/07/2015", "18/05/2015"];
const sorted = data.sort((a, b) => {
  const newA = a.split('/').reverse().join('-');
  const newB = b.split('/').reverse().join('-');
  return +new Date(newA) - +new Date(newB)
})
console.log(sorted)

We call data.sort with a function that gets the date strings and change their format to something that can used with the Date constructor.

Then we create date objects from each of them, convert them to timestamps, and subtract them to return the number to determine the sorting.

If the returned number is negative, then order stays the same. Otherwise, the a and b are reversed.

Therefore, sorted is ['18/05/2015', '09/06/2015', '22/06/2015', '25/06/2015', '25/07/2015'].

Conclusion

To sort a date string array with JavaScript, we can use the JavaScript date’s sort method.

Categories
JavaScript Answers

How to find the number of lines in an HTML text area with JavaScript?

Sometimes, we want to find the number of lines in an HTML text area with JavaScript.

In this article, we’ll look at how to find the number of lines in an HTML text area with JavaScript.

How to find the number of lines in an HTML text area with JavaScript?

To find the number of lines in an HTML text area with JavaScript, we can call the match method on the input value.

For instance, we write:

<textarea></textarea>

to add a text area.

Then we write:

const textarea = document.querySelector('textarea')
textarea.onchange = (e) => {
  console.log(e.target.value.match(/\n/g).length + 1)
}

to select the text area with querySelector.

Then we set textarea.onchange to a function that takes the input value from e.target.value.

Then we call match on it with the line break regex to find the number of line breaks.

And finally, we add 1 to that to get the number of lines.

Conclusion

To find the number of lines in an HTML text area with JavaScript, we can call the match method on the input value.

Categories
JavaScript Answers

How to get the second to last URL string segment with JavaScript?

Sometimes, we want to get the second to last URL string segment with JavaScript.

In this article, we’ll look at how to get the second to last URL string segment with JavaScript.

How to get the second to last URL string segment with JavaScript?

To get the second to last URL string segment with JavaScript, we can use the string’s split and array’s slice methods.

For instance, we write:

const url = 'http://www.example.com/website/projects/2'
const [secondLast] = url.split('/').slice(-2)
console.log(secondLast)

to split the url by the slashes with split.

Then we call slice to return an array with the last 2 entries of the URL segment string array.

Finally, we destructure the first entry from the returned array and assigned it to secondLast.

Therefore, secondLast is 'projects'.

Conclusion

To get the second to last URL string segment with JavaScript, we can use the string’s split and array’s slice methods.