Categories
JavaScript Answers

How to get array elements fulfilling a condition with JavaScript?

Sometimes, we want to get array elements fulfilling a condition with JavaScript.

In this article, we’ll look at how to get array elements fulfilling a condition with JavaScript.

How to get array elements fulfilling a condition with JavaScript?

To get array elements fulfilling a condition with JavaScript, we use the array filter method.

For instance, we write

const filterResult = [1, 16, 4, 6].filter((x) => {
  return x > 5;
});
console.log(filterResult);

to call [1, 16, 4, 6].filter with a function that checks if the item x being iterated through is bigger than 5.

If it’s bigger than 5, then it’ll be included in the returned array.

Conclusion

To get array elements fulfilling a condition with JavaScript, we use the array filter method.

Categories
JavaScript Answers

How to disable an option in a select based on its value with JavaScript?

Sometimes, we want to disable an option in a select based on its value with JavaScript.

In this article, we’ll look at how to disable an option in a select based on its value with JavaScript.

How to disable an option in a select based on its value with JavaScript?

To disable an option in a select based on its value with JavaScript, we can set its disabled property to true.

For instance, we write

document.querySelectorAll("#foo option").forEach((opt) => {
  if (opt.value === "example") {
    opt.disabled = true;
  }
});

to select the option elements in the drop down with querySelector.

Then we call forEach with a callback that checks if the value attribute’s value is 'example'.

If it is, then we set its disabled property to true to disable it.

Conclusion

To disable an option in a select based on its value with JavaScript, we can set its disabled property to true.

Categories
JavaScript Answers

How to change audio element src with JavaScript?

Sometimes, we want to change audio element src with JavaScript.

In this article, we’ll look at how to change audio element src with JavaScript.

How to change audio element src with JavaScript?

To change audio element src with JavaScript, we set the element’s src attribute.

For instance, we write

const audio = document.getElementById("audio");

const source = document.getElementById("audioSource");
source.src = elm.getAttribute("data-value");

audio.load();
audio.play();

to select the audio element with getElementById.

Then we get the element with the data-value attribute with getElementById.

We use the data-value attribute value for the src attribute’s value.

Next we call load to preload the audio element with the clip from the new URL.

Then we call play to play it.

Conclusion

To change audio element src with JavaScript, we set the element’s src attribute.

Categories
JavaScript Answers

How to stub a promise with sinon and JavaScript?

Sometimes, we want to stub a promise with sinon and JavaScript.

In this article, we’ll look at how to stub a promise with sinon and JavaScript.

How to stub a promise with sinon and JavaScript?

To stub a promise with sinon and JavaScript, we can return a promise with a stub.

For instance, we write

import sinon from "sinon";

const sandbox = sinon.sandbox.create();

const promiseResolved = () =>
  sandbox.stub().returns(Promise.resolve("resolved"));
const promiseRejected = () =>
  sandbox.stub().returns(Promise.reject("rejected"));

to call sandbox.stub to return a stub.

And we make it return a promise by calling returns with a promise.

Promise.resolve returns a promise that resolves.

And Promise.reject returns a promise that rejects.

Conclusion

To stub a promise with sinon and JavaScript, we can return a promise with a stub.

Categories
JavaScript Answers

How to put an array inside a JavaScript object?

Sometimes, we want to put an array inside a JavaScript object.

In this article, we’ll look at how to put an array inside a JavaScript object.

How to put an array inside a JavaScript object?

To put an array inside a JavaScript object, we set its as a property’s value.

For instance, we write

const defaults = {
  backgroundcolor: "#000",
  color: "#fff",
  weekdays: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"],
};

to set the default.weekdays property to the ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] array.

Conclusion

To put an array inside a JavaScript object, we set its as a property’s value.