Categories
JavaScript Answers

How to get range of items with JavaScript array?

Sometimes, we want to get range of items with JavaScript array.

In this article, we’ll look at how to get range of items with JavaScript array.

How to get range of items with JavaScript array?

To get range of items with JavaScript array, we use the slice method.

For instance, we write

const fruits = ["apple", "banana", "peach", "plum", "pear"];
const slice1 = fruits.slice(1, 3);

to call fruits.slice with index 1 and 3 to return an array with the items in fruits between indexes 1 and 2.

Conclusion

To get range of items with JavaScript array, we use the slice method.

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.