Categories
Books Vue 3 Vue 3 Projects

Buy Vue.js 3 By Example Now

Want to learn Vue 3 fast? Vue.js 3 By Example is out now.

Buy it now at https://www.packtpub.com/product/vue-js-3-by-example/9781838826345

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.

Categories
JavaScript Answers

How to sort an associative array by its values in JavaScript?

Sometimes, we want to sort an associative array by its values in JavaScript.

In this article, we’ll look at how to sort an associative array by its values in JavaScript.

How to sort an associative array by its values in JavaScript?

To sort an associative array by its values in JavaScript, we use some object methods.

For instance, we write

const sortedObj = Object.fromEntries(
  Object.entries(data).sort(([, val1], [, val2]) => val1 - val2)
);

to call Object.entries with data to return an array with the key-value pairs in their own arrays.

Then we call sort with a callback that gets the property values from each array and compare them to do the sorting and return the sorted array.

Then we call Object.fromEntries to convert the array of key-value pair arrays back into an object.

Conclusion

To sort an associative array by its values in JavaScript, we use some object methods.