Categories
JavaScript Answers

How to pass parameter to a promise function with JavaScript?

Sometimes, we want to pass parameter to a promise function with JavaScript.

In this article, we’ll look at how to pass parameter to a promise function with JavaScript.

How to pass parameter to a promise function with JavaScript?

To pass parameter to a promise function with JavaScript, we create a function that takes the parameter and returns the promise.

For instance, we write

const logUser = (username, password) => {
  return new Promise((resolve, reject) => {
    //...
    const response = "user logged in";
    resolve(response);
  });
};

logUser("user", "pass").then((respFromLogUser) => {
  showMessage(respFromLogUser);
});

to create the logUser function that returns a promise that uses the username and password parameters.

Then we call logInUser with the arguments and call then with a callback that has the resolved value set as the value of the callback parameter.

Conclusion

To pass parameter to a promise function with JavaScript, we create a function that takes the parameter and returns the promise.

Categories
JavaScript Answers

How to detect shift+enter and generate a new line in text area with JavaScript?

Sometimes, we want to detect shift+enter and generate a new line in text area with JavaScript.

In this article, we’ll look at how to detect shift+enter and generate a new line in text area with JavaScript.

How to detect shift+enter and generate a new line in text area with JavaScript?

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object.

For instance, we write

textArea.onKeyPress = (e) => {
  if (e.keyCode === 13 && e.shiftKey) {
    e.preventDefault();
    let start = e.target.selectionStart,
      end = e.target.selectionEnd;
    e.target.vaiue =
      prevState.message.substring(0, start) +
      "\n" +
      prevState.message.substring(end);
  } else if (e.keyCode === 13) {
    e.preventDefault();
  }
};

to check keyCode and shiftKey to see if shift+enter is pressed.

If it is, we call preventDefault to prevent the default behavior.

We then get the cursor’s start and end indexes with selectionStart and selectionEnd.

Then we put the new line in between the start and end with

prevState.message.substring(0, start) + "\n" + prevState.message.substring(end);

Conclusion

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object.

Categories
JavaScript Answers

How to use lodash to compare jagged arrays with JavaScript?

Sometimes, we want to use lodash to compare jagged arrays with JavaScript.

In this article, we’ll look at how to use lodash to compare jagged arrays with JavaScript.

How to use lodash to compare jagged arrays with JavaScript?

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

For instance, we write

const array1 = [
  ["a", "b"],
  ["b", "c"],
];
const array2 = [
  ["b", "c"],
  ["a", "b"],
];
_.isEqual(array1.sort(), array2.sort());

to call sort to sort both arrays.

And then we call Lodash’s isEqual function to compare the contents of the sorted arrays.

Conclusion

To use lodash to compare jagged arrays with JavaScript, we can sort the arrays with sort and then use isEqual to compare their contents.

Categories
JavaScript Answers

How to convert a string to JSON object with JavaScript?

Sometimes, we want to convert a string to JSON object with JavaScript.

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

How to convert a string to JSON object with JavaScript?

To convert a string to JSON object with JavaScript, we can use the JSON.parse method.

For instance, we write

const obj = JSON.parse(string);

to call JSON.parse with the JSON string to convert the JSON string to an object.

Conclusion

To convert a string to JSON object with JavaScript, we can use the JSON.parse method.

Categories
JavaScript Answers

How to add disabled attribute to input element using JavaScript?

Sometimes, we want to add disabled attribute to input element using JavaScript.

In this article, we’ll look at how to add disabled attribute to input element using JavaScript.

How to add disabled attribute to input element using JavaScript?

To add disabled attribute to input element using JavaScript, we can set the disabled property of the select element to true.

For instance, we write

<select id="selectFrom">
  ...
</select>

to add the select element.

then we write

document.getElementById("selectFrom").disabled = true;

to get the select element with getElementById.

Then we set its disabled property to true to make it disabled.

Conclusion

To add disabled attribute to input element using JavaScript, we can set the disabled property of the select element to true.