Skip to main content

Helpful JavaScript Utility Functions

· One min read
Nguyễn Huỳnh Minh Tiến

Nowadays, JavaScript is one of the most popular programming languages in the world. It is used by many companies such as Google, Facebook, Microsoft, etc. to build web applications. JavaScript is a very powerful language that can be used to build complex web applications. It is also a very flexible language that can be used to build simple web applications as well.

In this article, I will list out some of the most helpful JavaScript utility functions that I have used in my projects.

JavaScript

1. Get values by specific key from an object

This function will return an array of values that match the given key from an object.

Get all values of specific key in object
/**
* Get all values of specific key in object
* @param {object} obj A javascript object to get values
* @param {string} key A key of object
* @returns
*/
function GetValuesByKey(obj, key) {
let values = [];
for (let i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
values = values.concat(GetValuesByKey(obj[i], key));
} else if (i == key) {
values.push(obj[i]);
}
}
return values;
}

Share this page