Categories
JavaScript Answers

How to use expression inside switch case statement with JavaScript?

Sometimes, we want to use expression inside switch case statement with JavaScript.

In this article, we’ll look at how to use expression inside switch case statement with JavaScript.

How to use expression inside switch case statement with JavaScript?

To use expression inside switch case statement with JavaScript, we use switch with true.

For instance, we write

switch (true) {
  case amount >= 7500 && amount < 10000:
    // ...
    break;
  case amount >= 10000 && amount < 15000:
    // ...
    break;
  // ...
}

to use switch with true.

Then we can use any boolean expression with case to run code according to the amount value.

Conclusion

To use expression inside switch case statement with JavaScript, we use switch with true.

Categories
JavaScript Answers

How to splice an array in half, no matter the size with JavaScript?

Sometimes, we want to splice an array in half, no matter the size with JavaScript.

In this article, we’ll look at how to splice an array in half, no matter the size with JavaScript.

How to splice an array in half, no matter the size with JavaScript?

To splice an array in half, no matter the size with JavaScript, we get the index of the middle element in the array and use slice with that.

For instance, we write

const halfLength = Math.ceil(arrayName.length / 2);
const leftSide = arrayName.slice(0, halfLength);

to get the halfLength index.

We get the ceiling of the middle array length divide by 2 and use that with slice to get the first half the array.

The 2nd half we get by writing

const rightSide = arrayName.slice(halfLength);

Conclusion

To splice an array in half, no matter the size with JavaScript, we get the index of the middle element in the array and use slice with that.

Categories
JavaScript Answers

How to generate all combinations of elements in a single array in pairs with JavaScript?

Sometimes, we want to generate all combinations of elements in a single array in pairs with JavaScript.

In this article, we’ll look at how to generate all combinations of elements in a single array in pairs with JavaScript.

How to generate all combinations of elements in a single array in pairs with JavaScript?

To generate all combinations of elements in a single array in pairs with JavaScript, we use the array map and flatMap methods.

For instance, we write

const array = ["apple", "banana", "lemon", "mango"];

const result = array.flatMap((v, i) =>
  array.slice(i + 1).map((w) => v + " " + w)
);

console.log(result);

to call slice and map to create all the strings with 2 of the items in array in each string separated by a space.

Then we call flatMap to combine the nested arrays into one flat array.

Conclusion

To generate all combinations of elements in a single array in pairs with JavaScript, we use the array map and flatMap methods.

Categories
JavaScript Answers

How to flatten a JavaScript object keys and values to a single depth array?

To flatten a JavaScript object keys and values to a single depth array, we recursively put items into a new object with all the nested properties in the top level.

For instance, we write

const flattenObj = (obj, parent, res = {}) => {
  for (const key of Object.keys(obj)) {
    const propName = parent ? parent + "." + key : key;
    if (typeof obj[key] === "object" && obj[key] !== -null) {
      flattenObj(obj[key], propName, res);
    } else {
      res[propName] = obj[key];
    }
  }
  return res;
};

to define the flattenObj function.

In it, we get the keys of the obj object with Object.keys.

Then we loop through the keys with a for-of loop.

Then we define the propName by combining the parent property path string with the key.

Then if obj[key] is an object and it’s not null, we call flattenObj with it.

Otherwise we set obj[key] as the value of res[propName].

After the loop is done, res is returned.

Conclusion

To flatten a JavaScript object keys and values to a single depth array, we recursively put items into a new object with all the nested properties in the top level.

Categories
JavaScript Answers

How to check for palindrome in JavaScript?

Sometimes, we want to check for palindrome in JavaScript.

In this article, we’ll look at how to check for palindrome in JavaScript.

How to check for palindrome in JavaScript?

To check for palindrome in JavaScript, we check if the string and the reversed version of the string is the same.

For instance, we write

const checkPalindrome = (str) => {
  return str === str.split("").reverse().join("");
};

to define the checkPalindrome function.

In it, we get the reversed string with str.split("").reverse().join("").

And we check if it’s the same as the original str string with ===.

Conclusion

To check for palindrome in JavaScript, we check if the string and the reversed version of the string is the same.