Categories
JavaScript Answers

How to check for duplicate strings in JavaScript array?

Sometimes, we want to check for duplicate strings in JavaScript array.

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

How to check for duplicate strings in JavaScript array?

To check for duplicate strings in JavaScript array, we call the filter method.

For instance, we write

const strArray = ["q", "w", "w", "w", "e", "i", "u", "r"];
const findDuplicates = (arr) =>
  arr.filter((item, index) => arr.indexOf(item) !== index);

to define the findDuplicates method.

In it, we call arr.filter with a callback that checks if the first index of item isn’t index.

If they’re different, then the item is a duplicate.

Conclusion

To check for duplicate strings in JavaScript array, we call the filter method.

Categories
JavaScript Answers

How to search an array for a substring match with JavaScript?

Sometimes, we want to search an array for a substring match with JavaScript.

In this article, we’ll look at how to search an array for a substring match with JavaScript.

How to search an array for a substring match with JavaScript?

To search an array for a substring match with JavaScript, we call the filter and includes methods.

For instance, we write

const items = ["item 1", "thing", "id-3-text", "class"];
const matches = items.filter((s) => s.includes("thi"));

to call items.filter with a callback that checks if string s in items includes 'thi' with includes.

Categories
JavaScript Answers

How to find a value in an array of objects in JavaScript?

Sometimes, we want to find a value in an array of objects in JavaScript.

In this article, we’ll look at how to find a value in an array of objects in JavaScript.

How to find a value in an array of objects in JavaScript?

To find a value in an array of objects in JavaScript, we use the filter method.

For instance, we write

const people = [
  { name: "bob", dinner: "pizza" },
  { name: "john", dinner: "sushi" },
  { name: "larry", dinner: "hummus" },
];
const filterted = people.filter((person) => {
  return person.dinner === "sushi";
});

to call people.filter with a callback that checks if the items in people has dinner property equal to 'sushi' and return them in a new array.

Conclusion

To find a value in an array of objects in JavaScript, we use the filter method.

Categories
JavaScript Answers

How to reverse an array in JavaScript without using libraries?

Sometimes, we want to reverse an array in JavaScript without using libraries.

In this article, we’ll look at how to reverse an array in JavaScript without using libraries.

How to reverse an array in JavaScript without using libraries?

To reverse an array in JavaScript without using libraries, we use the reverse method.

For instance, we write

const original = [1, 2, 3, 4];
const reversed = [...original].reverse();

to make a copy of the original array with the spread operator.

Then we reverse the copied array with reverse.

Conclusion

To reverse an array in JavaScript without using libraries, we use the reverse method.

Categories
JavaScript Answers

How to filter an array with a function that returns a promise with JavaScript?

Sometimes, we want to filter an array with a function that returns a promise with JavaScript.

In this article, we’ll look at how to filter an array with a function that returns a promise with JavaScript.

How to filter an array with a function that returns a promise with JavaScript?

To filter an array with a function that returns a promise with JavaScript, we call filter with an async function.

For instance, we write

const results = await filter(myArray, async (num) => {
  await doAsyncStuff();
  return num > 2;
});

to call filter with myArray and an async function which always returns a promise.

An array of promises after filtering is done is returned.

Then we use await to get the array of promise results and assign the results to results.

Conclusion

To filter an array with a function that returns a promise with JavaScript, we call filter with an async function.