Categories
JavaScript Answers

How to create an associative array in JavaScript literal notation?

Sometimes, we want to create an associative array in JavaScript literal notation.

In this article, we’ll look at how to create an associative array in JavaScript literal notation.

How to create an associative array in JavaScript literal notation?

To create an associative array in JavaScript literal notation, we can use maps.

For instance, we write

const arr = new Map([
  ["key1", "User"],
  ["key2", "Guest"],
  ["key3", "Admin"],
]);

const res = arr.get("key2");
console.log(res);

to create a map by calling the Map constructor with a nested array of key-value pair arrays.

Then we calk get to get the value with the key 'key2'.

So res is 'Guest'.

Conclusion

To create an associative array in JavaScript literal notation, we can use maps.

Categories
JavaScript Answers

How to fix JSON.stringify not working with normal JavaScript array?

Sometimes, we want to fix JSON.stringify not working with normal JavaScript array.

In this article, we’ll look at how to fix JSON.stringify not working with normal JavaScript array.

How to fix JSON.stringify not working with normal JavaScript array?

To fix JSON.stringify not working with normal JavaScript array, we should make sure arrays have all numerical keys.

For instance, we write

const test = {};
test.a = "test";
test.b = [];
test.b.push("item");
test.b.push("item2");
test.b.push("item3");
const json = JSON.stringify(test);
console.log(json);

to create array properties a and b.

And we call push to push contents into it.

Then we call JSON.stringify to return the test object converted to a JSON string.

Conclusion

To fix JSON.stringify not working with normal JavaScript array, we should make sure arrays have all numerical keys.

Categories
JavaScript Answers

How to insert a large block of HTML in JavaScript?

Sometimes, we want to insert a large block of HTML in JavaScript.

In this article, we’ll look at how to insert a large block of HTML in JavaScript.

How to insert a large block of HTML in JavaScript?

To insert a large block of HTML in JavaScript, we use template literals.

For instance, we write

const a = 1;
const b = 2;
const div = document.createElement("div");
div.setAttribute("class", "post block bc2");
div.innerHTML = `
    <div class="parent">
        <div class="child">${a}</div>
        <div class="child">+</div>
        <div class="child">${b}</div>
        <div class="child">=</div>
        <div class="child">${a + b}</div>
    </div>
`;
document.getElementById("posts").appendChild(div);

to create the div with createElement.

Then we set its class attribute with setAttribute.

Next we set its content by setting the innerHTML property to the HTML we want to render.

We use template literals to interpolate values in the HTML.

And then we call appendChild to append the div as the last child of the element with ID posts.

Conclusion

To insert a large block of HTML in JavaScript, we use template literals.

Categories
JavaScript Answers

How to check if data attribute exist with plain JavaScript?

Sometimes, we want to check if data attribute exist with plain JavaScript.

In this article, we’ll look at how to check if data attribute exist with plain JavaScript.

How to check if data attribute exist with plain JavaScript?

To check if data attribute exist with plain JavaScript, we use the hasAttribute method.

For instance, we write

if (!object.hasAttribute("data-example-param")) {
  // ...
}

to check if object doesn’t have the data-example-param property.

hasAttribute returns true if the property with the given name exists and false otherwise.

Conclusion

To check if data attribute exist with plain JavaScript, we use the hasAttribute method.

Categories
JavaScript Answers

How to fix timeout feature in the axios library is not working with JavaScript?

Sometimes, we want to fix timeout feature in the axios library is not working with JavaScript.

In this article, we’ll look at how to fix timeout feature in the axios library is not working with JavaScript.

How to fix timeout feature in the axios library is not working with JavaScript?

To fix timeout feature in the axios library is not working with JavaScript, we use a cancel token.

For instance, we write

const source = CancelToken.source();
const timeout = setTimeout(() => {
  source.cancel();
}, 10000);

const result = await axios.get(ip + "/config", { cancelToken: source.token });
clearTimeout(timeout);

to call axios.get with the cancelToken we created with CancelToken.source.

Then we source.cancel in the setTimeout callback to cancel the get request when the request hasn’t finished in 10 seconds.

If the request is done before then, we use clearTimeout(timeout); to clear the timer.

Conclusion

To fix timeout feature in the axios library is not working with JavaScript, we use a cancel token.