Categories
JavaScript Answers

How to join or combine two arrays to concatenate into one JavaScript array?

To join or combine two arrays to concatenate into one JavaScript array, we call the concat method.

For instance, we write

const a = ["a", "b", "c"];
const b = ["d", "e", "f"];
const c = a.concat(b);

to call a.concat with b to return a new array with the entries in a followed by the entries in b.

Categories
JavaScript Answers

How to convert TypeScript enum to an object array?

To convert TypeScript enum to an object array, we use the Object.values method.

For instance, we write

enum Colors {
  WHITE = 0,
  BLACK = 1,
  BLUE = 3,
}

const colorValueArray = Object.values(Colors);

to call Object.values with the Colors enum to return the values in the enum as an array.

Categories
JavaScript Answers

How to remove leading comma from a string with JavaScript?

To remove leading comma from a string with JavaScript, we call the substring method.

For instance, we write

const myOriginalString = ",'first string','more','even more'";
const myString = myOriginalString.substring(1);

to call myOriginalString.substring with 1 to return a substring of myOriginalString without the first character.

Categories
JavaScript Answers

How to remove an element from a list, with JavaScript Lodash?

To remove an element from a list, with JavaScript Lodash, we use the remove method.

For instance, we write

_.remove(subTopics, (currentObject) => {
  return currentObject.subTopicId === stToDelete;
});

to call remove to remove the item in the subTopics array that has the subTopicID equals stToDelete by calling it with the callback that returns the condition.

Categories
JavaScript Answers

How to check if item is in array in JavaScript?

To check if item is in array in JavaScript, we use the indexOf method.

For instance, we write

const blockedTile = ["118", "67", "190", "43", "135", "520"];

if (blockedTile.indexOf("118") !== -1) {
  // ...
}

to call blockedFile.indexOf with the item we’re looking for to see it’s in the blockedFile array.

It returns the index of the item if it exists and -1 otherwise.

If it returns something bigger than -1, then the item is found.