Categories
JavaScript Answers

How to filter array of objects whose any properties contain a value with JavaScript?

To filter array of objects whose any properties contain a value with JavaScript, we call the filter method.

For instance, we write

const filterByValue = (array, string) => {
  return array.filter((o) =>
    Object.values(o).some((v) => v.toLowerCase().includes(string.toLowerCase()))
  );
};

const arrayOfObject = [
  { name: "Paul", country: "Canada" },
  { name: "Lea", country: "Italy" },
  { name: "John", country: "Italy" },
];

console.log(filterByValue(arrayOfObject, "lea"));

to define the filterByValue function.

In it, we call array.filter with a callback that calls Object.values with o to return an array of o‘s property values.

Then we call some with the array to check if v includes string if they’re both converted to lower case with toLowerCase with includes.

Next, we call filterByValue with arrayOfObject and the value we’re checking for.

Categories
JavaScript Answers

How to check if a string contains any element of an array in JavaScript?

To check if a string contains any element of an array in JavaScript, we call the filter method.

For instance, we write

const arr = ["banana", "monkey banana", "apple", "kiwi", "orange"];

const checker = (value) =>
  !["banana", "apple"].some((element) => value.includes(element));

console.log(arr.filter(checker));

to call arr.filter with the checker function.

In checker, we check if there’re any strings that include 'banana' or 'apple' in value in the arr array with includes.

some returns true if its callback returns true.

So we negate it to find the values in arr that don’t include 'banana' or 'apple'.

filter returns an array with the strings that don’t include 'banana' or 'apple'.

Categories
JavaScript Answers

How to find data in nested arrays with JavaScript?

To find data in nested arrays with JavaScript, we use the find and some methods.

For instance, we write

const products = [
  {
    id: 01,
    items: [
      {
        id: 01,
        name: "apple",
      },
      {
        id: 02,
        name: "banana",
      },
      {
        id: 03,
        name: "orange",
      },
    ],
  },
  {
    id: 02,
    items: [
      {
        id: 01,
        name: "carrot",
      },
      {
        id: 02,
        name: "lettuce",
      },
      {
        id: 03,
        name: "peas",
      },
    ],
  },
  {
    id: 03,
    items: [
      {
        id: 01,
        name: "eggs",
      },
      {
        id: 02,
        name: "bread",
      },
      {
        id: 03,
        name: "milk",
      },
    ],
  },
];
const product = products.find((product) =>
  product.items.some((item) => item.name === "milk")
);

to call products.find with a callback that checks if any object in the items array property has the name property equal to 'milk' with some.

some returns true if any item matches the condition we return in the callback.

find returns the first item where some returns true.

Categories
JavaScript Answers

How to find last index of element inside array by certain condition with JavaScript?

To find last index of element inside array by certain condition with JavaScript, we use the map and lastIndexOf methods.

For instance, we write

const lastIndex = elements.map((e) => e.a).lastIndexOf("something");

to call map with a callback that returns property a‘s value in each object e in elements.

Then we call lastIndexOf to get the last index of the a value that equals 'something' in the returned array.

Categories
JavaScript Answers

How to add default array values with JavaScript?

To add default array values with JavaScript, we call the fill method.

For instance, we write

const arr = Array(100).fill(0);

to define an array with 100 empty slots with Array(100).

Then we call fill with 0 to fill the empty slots with 0’s.