Categories
JavaScript Answers

How to filter strings in array based on content with JavaScript?

Sometimes, we want to filter strings in array based on content with JavaScript.

In this article, we’ll look at how to filter strings in array based on content with JavaScript.

How to filter strings in array based on content with JavaScript?

To filter strings in array based on content with JavaScript, we can use the JavaScript array’s filter instance method.

For instance, we write:

const myArray = ["bedroomone", "bedroomonetwo", "bathroom"];
const pattern = /bedroom/;
const filtered = myArray.filter((str) => {
  return pattern.test(str);
});
console.log(filtered)

We call myArray.filter with a callback that matches all the strings that has 'bedroom' in it by calling pattern.test with str.

Therefore, filtered is ['bedroomone', 'bedroomonetwo'].

Conclusion

To filter strings in array based on content with JavaScript, we can use the JavaScript array’s filter instance method.

Categories
JavaScript Answers

How to a remove portion of string in JavaScript?

Sometimes, we want to a remove portion of string in JavaScript.

In this article, we’ll look at how to a remove portion of string in JavaScript.

How to a remove portion of string in JavaScript?

To a remove portion of string in JavaScript, we can call the string replace method with the substring we want to remove and replace it with an empty string.

For instance, we write:

const string = '@usernameWhats on your mind?'
const newString = string.replace('Whats on your mind?', '');
console.log(newString)

We call string.replace to replace 'Whats on your mind?' with an empty string and return the new string.

Therefore, newString is '@username'.

Conclusion

To a remove portion of string in JavaScript, we can call the string replace method with the substring we want to remove and replace it with an empty string.

Categories
JavaScript Answers

How to create label and check box dynamically in JavaScript?

Sometimes, we want to create label and check box dynamically in JavaScript.

In this article, we’ll look at how to create label and check box dynamically in JavaScript.

How to create label and check box dynamically in JavaScript?

To create label and check box dynamically in JavaScript, we can use the createElement method.

For instance, we write:

const newLabel = document.createElement("label");
newLabel.setAttribute("for", 'checkbox');
newLabel.innerHTML = "Here goes the text";

const newCheckbox = document.createElement("input");
newCheckbox.setAttribute("type", 'checkbox');
newCheckbox.setAttribute("id", 'checkbox');

document.body.appendChild(newLabel);
document.body.appendChild(newCheckbox);

We call document.createElement to create a label.

Then we call setAttribute to set the for attribute to checkbox.

And we set the innerHTML property to add some text to the label.

Next, we call createElement again to create an input.

Next, we call setAttribute to set the type attribute to checkbox to make it a checkbox.

We call the same method to set the id attribute.

Finally, we call document.body.appendChild to append them both to the body element.

Conclusion

To create label and check box dynamically in JavaScript, we can use the createElement method.

Categories
JavaScript Answers Nodejs

How to add to an existing JSON file in Node.js?

Sometimes, we want to add to an existing JSON file in Node.js.

In this article, we’ll look at how to add to an existing JSON file in Node.js.

How to add to an existing JSON file in Node.js?

To add to an existing JSON file in Node.js, we can use the fs.readFile method.

For instance, we have:

["foo","bar"]

in results.json

Then we write:

const { promises: fs } = require("fs");
const currentSearchResult = 'example'

const write = async () => {
  const data = await fs.readFile('results.json')
  const json = JSON.parse(data)
  json.push(currentSearchResult)
  await fs.writeFile("results.json", JSON.stringify(json))
}
write()

to call fs.readFile to read results.json into data.

Then we call JSON.parse with data to parse data into an onject.

Then we call json.push with currentSearchResult to add currentSearchResult to json assuming json is an array.

Finally, we call fs.writeFile with the file path and the content to write to the file.

Now result.json has:

["foo","bar","example"]

Conclusion

To add to an existing JSON file in Node.js, we can use the fs.readFile method.

Categories
JavaScript Answers

How to convert a JavaScript timestamp into UTC format?

Sometimes, we want to convert a JavaScript timestamp into UTC format.

In this article, we’ll look at how to convert a JavaScript timestamp into UTC format.

How to convert a JavaScript timestamp into UTC format?

To convert a JavaScript timestamp into UTC format, we can call the toUTCString method.

For instance, we write:

const s = (new Date(1291656749000)).toUTCString()
console.log(s)

We create a date with a timestamp.

Then we call toUTCString to return the UTC date string from the date.

As a result, s is 'Mon, 06 Dec 2010 17:32:29 GMT'.

Conclusion

To convert a JavaScript timestamp into UTC format, we can call the toUTCString method.