Categories
JavaScript Answers

How to filter array of objects with JavaScript?

Sometimes, we want to filter array of objects with JavaScript.

In this article, we’ll look at how to filter array of objects with JavaScript.

How to filter array of objects with JavaScript?

To filter array of objects with JavaScript, we call the filter method.

For instance, we write

const foundNames = names.filter((v) => v.name === "Joe" && v.age < 30);

to call names.filter with a callback that checks if the name property is 'Joe' and the age property is less than 30.

Then objects that meet these conditions are returned in a new array.

Conclusion

To filter array of objects with JavaScript, we call the filter method.

Categories
JavaScript Answers

How to check if array contains all elements of another array with JavaScript?

Sometimes, we want to check if array contains all elements of another array with JavaScript.

In this article, we’ll look at how to check if array contains all elements of another array with JavaScript.

How to check if array contains all elements of another array with JavaScript?

To check if array contains all elements of another array with JavaScript, we call the every method.

For instance, we write

const array1 = [1, 2, 3];
const array2 = [1, 2, 3, 4];

const checker = (arr, target) => target.every((v) => arr.includes(v));
console.log(checker(array2, array1));

to call target.every`` with a callback that checks if every item vintargetis in thearrarray withincludes`.

Then we call checker with the 2 arrays to check.

Conclusion

To check if array contains all elements of another array with JavaScript, we call the every method.

Categories
JavaScript Answers

How to find index of an object by key and value in a JavaScript array?

To find index of an object by key and value in a JavaScript array, we use the findIndex method.

For instance, we write

const index = peoples.findIndex((p) => p.attr1 == "john");

to call peoples.findIndex with a callback that checks if attr1 property of each object in peoples is 'john'.

The first index of the found item is returned.

Categories
JavaScript Answers

How to add key value pair to all objects in array with JavaScript?

To add key value pair to all objects in array with JavaScript, we use the spread operator.

For instance, we write

const arr = [{ name: "eve" }, { name: "john" }, { name: "jane" }];
const newArr = arr.map((v) => ({ ...v, isActive: true }));

to call arr.map with a callback that returns an object with the properties in v spread into a new object.

And then we add the isActive property to the object.

Categories
JavaScript Answers

How to declare an empty two-dimensional array in JavaScript?

To declare an empty two-dimensional array in JavaScript, we use the map method.

For instance, we write

const arr = [...Array(3)].map(() => Array(5));

to create an array with 3 empty slots with Array(3).

And then we spread them to a new array.

Then we call map with a callback that returns an array with 5 empty slots created with Array(5).

We now have a 3×5 empty 2d array.