Categories
JavaScript Answers

How to split string variable from last slash with JavaScript?

Sometimes, we want to split string variable from last slash with JavaScript.

In this article, we’ll look at how to split string variable from last slash with JavaScript.

How to split string variable from last slash with JavaScript?

To split string variable from last slash with JavaScript, we can use the string lastIndexOf method.

For instance, we write

const rest = str.substring(0, str.lastIndexOf("/") + 1);
const last = str.substring(str.lastIndexOf("/") + 1, str.length);

to call substring with the index of the last / with lastIndexOf.

We get the substring before the last / with str.substring(0, str.lastIndexOf("/") + 1).

And we get the substring from the last / to the end with str.substring(str.lastIndexOf("/") + 1, str.length).

Conclusion

To split string variable from last slash with JavaScript, we can use the string lastIndexOf method.

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.