Categories
JavaScript Answers

How to remove key and value from hash in JavaScript?

To remove key and value from hash in JavaScript, we use the delete operator.

For instance, we write

delete myHash['key2']

to remove the property with key key2 from the myHash object with the delete operator.

Categories
JavaScript Answers

How to join an array by a comma and a space with JavaScript?

To join an array by a comma and a space with JavaScript, we use the join method.

For instance, we write

const myArray = [
  "css",
  "html",
  "xhtml",
  "html5",
  "css3",
  "javascript",
  "jquery",
  "lesscss",
  "arrays",
  "wordpress",
  "facebook",
  "fbml",
  "table",
  ".htaccess",
  "php",
  "c",
  ".net",
  "c#",
  "java",
];
const myString = myArray.join(", ");

to call myArray.join with ', ' to combine the strings in myArray into a string with the entries separated by ', '.

Categories
JavaScript Answers

How to sort mixed alpha/numeric array with JavaScript?

To sort mixed alpha/numeric array with JavaScript, we use the localeCompare method.

For instance, we write

const sortAlphaNum = (a, b) => a.localeCompare(b, "en", { numeric: true });
const arr = [
  "A1",
  "A10",
  "A11",
  "A12",
  "A2",
  "A3",
  "A4",
  "B10",
  "B2",
  "F1",
  "F12",
  "F3",
];
const sorted = arr.sort(sortAlphaNum);

to define the sortAlphaNum function that returns the result of localeCompare to compare a and b.

We set numeric to true compare numeric values.

Then we call arr.sort with sortAlphaNum to return an array with the values in arr sorted.

Categories
JavaScript Answers

How to get column from a two-dimensional array with JavaScript?

Sometimes, we want to get column from a two-dimensional array with JavaScript.

In this article, we’ll look at how to get column from a two-dimensional array with JavaScript.

How to get column from a two-dimensional array with JavaScript?

To get column from a two-dimensional array with JavaScript, we use the map method.

For instance, we write

const twoD = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];
const col3 = twoD.map((value, index) => {
  return value[2];
});

to call twoD.map with a callback that gets the 3rd item from each nested array with value[2].

An array with the vales returned by the callback is returned.

Conclusion

To get column from a two-dimensional array with JavaScript, we use the map method.

Categories
JavaScript Answers

How to use promise function inside JavaScript array map?

Sometimes, we want to use promise function inside JavaScript array map.

In this article, we’ll look at how to use promise function inside JavaScript array map.

How to use promise function inside JavaScript array map?

To use promise function inside JavaScript array map, we use the Promise.all method.

For instance, we write

const mappedArray = await Promise.all(
  array.map((p) => {
    return getPromise(p).then((i) => i.Item);
  })
);

to call Promise.all with an array of promises returned by map to invoke all the promises in the returned array in parallel.

And we use await to get an array all the results from the promises in the array.

Conclusion

To use promise function inside JavaScript array map, we use the Promise.all method.