Categories
JavaScript Answers

How to convert a JavaScript object to a query string?

Sometimes, we want to convert a JavaScript object to a query string.

In this article, we’ll look at how to convert a JavaScript object to a query string.

How to convert a JavaScript object to a query string?

To convert a JavaScript object to a query string, we can use the Object.entries method and some array methods.

For instance, we write:

const obj = {
  foo: "22",
  bar: "23434"
};

const queryString = Object.entries(obj)
  .map(([key, value]) => {
    return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  })
  .join('&')
console.log(queryString)

to call Object.entries with obj to convert obj to an array of key-value pair arrays.

Then we call map with a callback to combine the key and value into a query parameter string.

Next, we call join with '&' to join the query parameters together.

As a result, queryString is 'foo=22&bar=23434'.

Conclusion

To convert a JavaScript object to a query string, we can use the Object.entries method and some array methods.

Categories
JavaScript Answers

How to write the equivalent of Python dictionary’s get method in JavaScript?

Sometimes, we want to write the equivalent of Python dictionary’s get method in JavaScript.

In this article, we’ll look at how to write the equivalent of Python dictionary’s get method in JavaScript.

How to write the equivalent of Python dictionary’s get method in JavaScript?

To write the equivalent of Python dictionary’s get method in JavaScript, we can use hasOwnProperty.

For instance, we write:

const obj = {
  foo: 1
}
const x = obj.hasOwnProperty("foo") ? obj.foo : "default";
const y = obj.hasOwnProperty("bar") ? obj.bar : "default";
console.log(x, y)

to call obj.hasOwnProperty with the keys we’re looking for in obj.

If hasOwnProperty returns true, that means the key is in obj.

And so we return the property’s value.

Otherwise, we return 'default'.

Therefore, x is 1 and y is default.

Conclusion

To write the equivalent of Python dictionary’s get method in JavaScript, we can use hasOwnProperty.

Categories
JavaScript Answers

How to write a JavaScript regex for number with decimals and thousand separator?

Sometimes, we want to write a JavaScript regex for number with decimals and thousand separator.

In this article, we’ll look at how to write a JavaScript regex for number with decimals and thousand separator.

How to write a JavaScript regex for number with decimals and thousand separator?

To write a JavaScript regex for number with decimals and thousand separator, we can use /^\d{1,3}(,\d{3})*(\.\d+)?$/.

^\d{1,3} matches groups of digits up to 3 digits long at the start of the string.

We match the digit groups in the middle of the string with (,\d{3})*.

And we match the digit group at the end of the string with (\.\d+)?$.

Conclusion

To write a JavaScript regex for number with decimals and thousand separator, we can use /^\d{1,3}(,\d{3})*(\.\d+)?$/.

Categories
JavaScript Answers

How to convert a 32 bit integer into 4 bytes of data in JavaScript?

Sometimes, we want to convert a 32 bit integer into 4 bytes of data in JavaScript.

In this article, we’ll look at how to convert a 32 bit integer into 4 bytes of data in JavaScript.

How to convert a 32 bit integer into 4 bytes of data in JavaScript?

To convert a 32 bit integer into 4 bytes of data in JavaScript, we can use the ArrayBuffer and DataView constructors.

For instance, we write:

const toBytesInt32 = (num) => {
  const arr = new ArrayBuffer(4);
  const view = new DataView(arr);
  view.setUint32(0, num, false);
  return arr;
}

console.log(toBytesInt32((57586868).toString(2)))

to define the toBytesInt32 function that takes the num string.

In it, we create an ArrayBuffer with 4 to create a 4 bytes array buffer.

Then create a DataView instance with the array buffer.

And then we populate the view with num.

Conclusion

To convert a 32 bit integer into 4 bytes of data in JavaScript, we can use the ArrayBuffer and DataView constructors.

Categories
JavaScript Answers

How to add a text link inside a div using JavaScript?

Sometimes, we want to add a text link inside a div using JavaScript.

In this article, we’ll look at how to add a text link inside a div using JavaScript.

How to add a text link inside a div using JavaScript?

To add a text link inside a div using JavaScript, we can set the innerHTML property of the div to a string with the link code.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

const div = document.querySelector('div')
div.innerHTML = '<a href="http://www.example.com">example</a>'

to select the div with document.querySelector.

And then we set its innerHTML property to the link string.

Now we should see the link displayed on screen.

Conclusion

To add a text link inside a div using JavaScript, we can set the innerHTML property of the div to a string with the link code.