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.

Categories
JavaScript Answers

How to find elements in array that are not in another array with JavaScript?

Sometimes, we want to find elements in array that are not in another array with JavaScript.

In this article, we’ll look at how to find elements in array that are not in another array with JavaScript.

How to find elements in array that are not in another array with JavaScript?

To find elements in array that are not in another array with JavaScript, we use the filter and includes method.

For instance, we write

const a1 = ["a", "b", "c", "t"];
const a2 = ["d", "a", "t", "e", "g"];

console.log(a2.filter((x) => !a1.includes(x)));

to call filter with a callback that checks if item x in a2 isn’t a1 with includes and negation.

An array with the items in a2 not in a1 is returned.

Conclusion

To find elements in array that are not in another array with JavaScript, we use the filter and includes method.

Categories
JavaScript Answers

How to extract the property values of a JavaScript object into an array?

Sometimes, we want to extract the property values of a JavaScript object into an array.

In this article, we’ll look at how to extract the property values of a JavaScript object into an array.

How to extract the property values of a JavaScript object into an array?

To extract the property values of a JavaScript object into an array, we use the Object.values method.

For instance, we write

const dataObject = {
  object1: {
    id: 1,
    name: "Fred",
  },
  object2: {
    id: 2,
    name: "Wilma",
  },
  object3: {
    id: 3,
    name: "Pebbles",
  },
};

const valuesOnly = Object.values(dataObject);

to call Object.values with dataObject to return the property values of dataObject in an array.

Conclusion

To extract the property values of a JavaScript object into an array, we use the Object.values method.