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.

Categories
JavaScript Answers

How to declare an array in JavaScript?

To declare an array in JavaScript, we use array literals.

For instance, we write

const x = [];
const y = [10];

const z = [];
z[2] = 12;

to declare array x which is an empty array.

We declare y which is an array with 10 in it.

And we declare array z as an empty array and then set 12 as the 3rd element in z.

Categories
JavaScript Answers

How to count the number of times a same value appears in a JavaScript array?

To count the number of times a same value appears in a JavaScript array, we call the filter method.

For instance, we write

const getOccurrence = (array, value) => {
  return array.filter((v) => v === value).length;
};

console.log(getOccurrence(arr, 1));

to call array.filter with a callback that checks if v in array equals to value.

An array of values in array that equals to value is returned.

Then we get the count using the length property.