Categories
JavaScript Answers

How to remove empty strings from array while keeping record without loops with JavaScript?

Sometimes, we want to remove empty strings from array while keeping record without loops with JavaScript.

In this article, we’ll look at how to remove empty strings from array while keeping record without loops with JavaScript.

How to remove empty strings from array while keeping record without loops with JavaScript?

To remove empty strings from array while keeping record without loops with JavaScript, we call the filter method.

For instance, we write

const arr = ["I", "am", "", "still", "here", "", "man"];
const newArr = arr.filter(Boolean);

to call arr.filter with Boolean to return a new array without the falsy values in arr.

Empty string is a falsy value so it’s filtered out.

Conclusion

To remove empty strings from array while keeping record without loops with JavaScript, we call the filter method.

Categories
JavaScript Answers

How to remove undefined values from array with JavaScript?

Sometimes, we want to remove undefined values from array with JavaScript.

In this article, we’ll look at how to remove undefined values from array with JavaScript.

How to remove undefined values from array with JavaScript?

To remove undefined values from array with JavaScript, we call the filter method.

For instance, we write

const data = [42, 21, undefined, 50, 40, undefined, 9];

const newData = data.filter((element) => {
  return element !== undefined;
});

to call data.filter with a callback that checks if each element in data isn’t undefined.

Then items that aren’t undefined are returned in a new array.

Conclusion

To remove undefined values from array with JavaScript, we call the filter method.

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.