Categories
JavaScript Answers

How to filter array of objects with Lodash based on property value with JavaScript?

Sometimes, we want to filter array of objects with Lodash based on property value with JavaScript.

In this article, we’ll look at how to filter array of objects with Lodash based on property value with JavaScript.

How to filter array of objects with Lodash based on property value with JavaScript?

To filter array of objects with Lodash based on property value with JavaScript, we use the filter method.

For instance, we write

const myArr = [
  { name: "john", age: 23 },
  { name: "john", age: 43 },
  { name: "jim", age: 101 },
  { name: "bob", age: 67 },
];

const johnArr = _.filter(myArr, (person) => person.name === "john");
console.log(johnArr);

to call filter with myArr with a callback that checks if person.name is 'john' to return an array that has name property set to 'john'.

Conclusion

To filter array of objects with Lodash based on property value with JavaScript, we use the filter method.

Categories
JavaScript Answers

How to use JavaScript map with a function that has two arguments?

Sometimes, we want to use JavaScript map with a function that has two arguments.

In this article, we’ll look at how to use JavaScript map with a function that has two arguments.

How to use JavaScript map with a function that has two arguments?

To use JavaScript map with a function that has two arguments, we call map with a function that calls the function that takes 2 arguments.

For instance, we write

const values = [1, 2, 3, 4];
const mapped = values.map((n) => square(n, 3));

to call values.map with a function that calls square with n and 3.

n is the value in values being iterated through.

Conclusion

To use JavaScript map with a function that has two arguments, we call map with a function that calls the function that takes 2 arguments.

Categories
JavaScript Answers

How to fix date parsing in JavaScript is different between Safari and Chrome?

Sometimes, we want to fix date parsing in JavaScript is different between Safari and Chrome.

In this article, we’ll look at how to fix date parsing in JavaScript is different between Safari and Chrome.

How to fix date parsing in JavaScript is different between Safari and Chrome?

To fix date parsing in JavaScript is different between Safari and Chrome, we should make sure the date string is in the right format.

For instance, we write

const c = new Date("2022-02-06T20:00:00.000+04:00");
console.log(c);

to create a new Date object with a date string in a format that can be pared by both browsers.

We have '+04:00' for the time zone offset which can be parsed correctly by both browsers.

Conclusion

To fix date parsing in JavaScript is different between Safari and Chrome, we should make sure the date string is in the right format.

Categories
JavaScript Answers

How to convert string to number in JavaScript?

Sometimes, we want to convert string to number in JavaScript.

In this article, we’ll look at how to convert string to number in JavaScript.

How to convert string to number in JavaScript?

To convert string to number in JavaScript, we use the Number function.

For instance, we write

const str = "1";
const num = Number(str);

to call Number with str to return 1.

Conclusion

To convert string to number in JavaScript, we use the Number function.

Categories
JavaScript Answers

How to check if a string ends with a substring with JavaScript?

Sometimes, we want to check if a string ends with a substring with JavaScript.

In this article, we’ll look at how to check if a string ends with a substring with JavaScript.

How to check if a string ends with a substring with JavaScript?

To check if a string ends with a substring with JavaScript, we can use the string endsWith method.

For instance, we write

const endsWithDog = "this is dog".endsWith("dog");

to call "this is dog".endsWith with 'dog' to check if "this is dog" ends with 'dog'.

It should return true since 'dog' is at the end of "this is dog".

Conclusion

To check if a string ends with a substring with JavaScript, we can use the string endsWith method.