Categories
JavaScript Answers

How to check if array has elements from another array with JavaScript?

Sometimes, we want to check if array has elements from another array with JavaScript.

In this article, we’ll look at how to check if array has elements from another array with JavaScript.

How to check if array has elements from another array with JavaScript?

To check if array has elements from another array with JavaScript, we can use the array some and includes method.

For instance, we write:

const a = [1, 2, 3]
const b = [1, 4, 5]
const includesB = a.some(e => b.includes(e))
console.log(includesB)

to call a.some with a callback that returns b.includes(e) to check any element in a is also in b.

Since 1 is in a and b, includesB is true.

Conclusion

To check if array has elements from another array with JavaScript, we can use the array some and includes method.

Categories
JavaScript Answers

How to find if object property exists in array with JavaScript and Lodash?

Sometimes, we want to find if object property exists in array with JavaScript and Lodash.

In this article, we’ll look at how to find if object property exists in array with JavaScript and Lodash.

How to find if object property exists in array with JavaScript and Lodash?

To find if object property exists in array with JavaScript and Lodash, we can use the find method.

For instance, we write:

const data = [{
    "name": "apple",
    "id": "apple0"
  },
  {
    "name": "dog",
    "id": "dog1"
  },
  {
    "name": "cat",
    "id": "cat2"
  }
]

const apple = _.find(data, {
  name: 'apple'
})
console.log(apple)

to call _.find with data with the property key and value we’re looking for.

Therefore, apple is {name: 'apple', id: 'apple0'}.

Conclusion

To find if object property exists in array with JavaScript and Lodash, we can use the find method.

Categories
JavaScript Answers

How to convert a number to a letter with JavaScript?

Sometimes, we want to convert a number to a letter with JavaScript.

In this article, we’ll look at how to convert a number to a letter with JavaScript.

How to convert a number to a letter with JavaScript?

To convert a number to a letter with JavaScript, we can use the String.fromCharCode method.

For instance, we write:

const i = 5
const c = String.fromCharCode(94 + i);
console.log(c)

We call String.fromCharCode with 94 + i to convert i to a letter.

Therefore, c is 'c' since 97 is the code for 'c'‘ in ASCII.

Conclusion

To convert a number to a letter with JavaScript, we can use the String.fromCharCode method.

Categories
JavaScript Answers

How to round up to the nearest 0.05 in JavaScript?

Sometimes, we want to round up to the nearest 0.05 in JavaScript.

In this article, we’ll look at how to round up to the nearest 0.05 in JavaScript.

How to round up to the nearest 0.05 in JavaScript?

To round up to the nearest 0.05 in JavaScript, we can use the Math.ceil and toFixed methods.

For instance, we write:

const number = 20.22
const rounded = (Math.ceil(number * 20) / 20).toFixed(2)
console.log(rounded)

to multiply number by 20.

Then we round that up with Math.ceil.

Next, we divide the rounded number by 20.

And then we call toFixed with 2 to return the number as a string rounded to 2 decimal places.

Therefore, rounded is "20.25".

Conclusion

To round up to the nearest 0.05 in JavaScript, we can use the Math.ceil and toFixed methods.

Categories
JavaScript Answers

How to sort array on key value with JavaScript?

Sometimes, we want to sort array on key value with JavaScript.

In this article, we’ll look at how to sort array on key value with JavaScript.

How to sort array on key value with JavaScript?

To sort array on key value with JavaScript, we can use the array sort method.

For instance, we write:

const arr = [{
    name: 'bob',
    artist: 'rudy'
  },
  {
    name: 'johhny',
    artist: 'drusko'
  },
  {
    name: 'tiff',
    artist: 'needell'
  },
  {
    name: 'top',
    artist: 'gear'
  }
];

const sorted = arr.sort((a, b) => a.name.localeCompare(b.name))
console.log(sorted)

to call arr.sort with a callback that sort the entries by the name property lexically with localeCompare.

Therefore, sorted is:

[{
  artist: "rudy",
  name: "bob"
}, {
  artist: "drusko",
  name: "johhny"
}, {
  artist: "needell",
  name: "tiff"
}, {
  artist: "gear",
  name: "top"
}]

Conclusion

To sort array on key value with JavaScript, we can use the array sort method.