Tag Archive for: Key



There are multiple ways of storing key => value arrays in JavaScript. However, the tricky part is storing the keys and values from two different arrays into a single element. And to add to its trickiness, key and value are to be stored in such a scheme that fetching a key with its respective value is easy. This cuts down the number of methods to achieve the task at hand to only two. The two most promising methods include the use of Objects and Maps. This article will go through both of these methods one by one.

Note: This article will assume that keys and values are stored in different arrays, and the objective is to store them together and have them formatted as “key => value” while fetching.

Method 1: Using Objects to Store Key => Value Arrays

To demonstrate this method, first create a key array and a value array with the following lines:

var keysArray = [«China», «England», «Egypt», «Finland», «Greece»];
var valuesArray = [«Beijing», «London», «Cairo», «Helsinki», «Athens»];

After that, create an empty JavaScript object with the following line:

After that, simply copy the keys and values from their array and add them in the object using the following lines:

for (var i = 0; i < keysArray.length; i++) {
  resultObj[keysArray[i]] = valuesArray[i];
}

In this above code snippet:

  • A for loop is run and its iterations are equal to the number of elements inside the keys array.
  • In each iteration, a new attribute of property of the object is created, and it is given the name equal to the element inside the key array and its respective value from the value array by using the same index values.

After that, pass the resultObj to the console log function to print it out on the terminal:

Executing the code will provide the following output:

The keys and values are stored together, but they are still not in the “key => format
To display them in the correct format, use the following lines of code:

for (x of Object.keys(resultObj)) {
  console.log(x + » => « + resultObj[x]);
}

In this code snippet:

  • Object.keys() method returns the keys of the object in its argument one by one. The keys are getting stored inside the variable “x
  • String concatenation is used to format the output of the console log as “keys=> values”

Executing the program now produces the following result:

The output shows that the keys are not only stored together but also formatted in the correct way.

Method 2: Using Maps to Store Key => Value Arrays

To demonstrate the usage of maps for storing keys and their respective values, create two arrays with keys and values with the following lines of code:

var keysArray = [«China», «England», «Egypt», «Finland», «Greece»];
var valuesArray = [«Beijing», «London», «Cairo», «Helsinki», «Athens»];

The next step is to create a map, for this create a variable and set it equal to the new Map() constructor like:

To add values to a Map variable, there is this method mapVar.set(). Use this function to add keys and their respective values:

for (i = 0; i < keysArray.length; i++) {
  resultMap.set(keysArray[i], valuesArray[i]);
}

In the code snippet mentioned above:

  • A for loop is used to iterate through the keysArray and the valuesArray using the length of the keysArray.
  • In each iteration, resultMap.set() method is used to store the key and value pair in the map.

After this, simply pass the resultMap variable onto the terminal by using the console log function:

This code will produce the following output:

It is sort of in the right format, but it includes a little extra information. To correctly format it, use the following lines:

for (key of resultMap.keys()) {
  console.log(key + » => « + resultMap.get(key));
}

In this code snippet:

  • resultMap.keys() method returns the keys of the map one by one to the key variable.
  • resultMap.get() method is used to get the value of a specific key.
  • And in the console log function, string concatenation is used to correctly format the output.

Executing the code now produces the following output on the terminal:

The output shows that the keys are not only stored together but also formatted in the correct way.

Conclusion

In JavaScript, Objects and Maps are the two elements that are most suited to store keys and value pairs, even if the task at hand is to take keys and values from individual arrays and place them inside a single entity. Afterward, whenever the user is trying to get keys and their respective values, they can be easily formatted in “key => value” format by using simple string concatenation in JavaScript.



Source link


There are multiple ways of checking the existing keys in a JavaScript object. Most of the ways include using methods from other packages. To do that, one generally has to first install that package and then work with the methods written inside it. But in this article, we will be working with the methods that come as default in JavaScript. So, let’s start with the first method.

Method 1: Using the “in” Operator to Find the Existence of a Key

We can use the “in” operator to check for a particular key in an object, just like we can use it to find the existence of a particular character in a string. To demonstrate this, we are going to need an object there create an object with the following lines of code:

var personObject = {
    firstName: «John»,
    lastName: «Doe»,
    age: 18,
    salary: 2200
}

 
As you can see, this object is about a person and includes details like the first name, last name, age, and salary. Suppose that we want to check whether or not the key “age” is present in our personObject. In that case, search for age in personObject and set the return value in a new variable:

existence = «age» in personObject;

 
After that, we can simply print the value inside the existence variable on the terminal using the console log function like:

 
After that, simply execute the program and observe the following result on the terminal:


The true value in the terminal means that the key age does exist in the object personObject.

After that, we also want to check for a key that is not present in the personObject. For this, we are going to use the in operator to find the key “martialStatus” in the personObject like:

existence = «martialStatus» in personObject;

 
And then again, we can simply pass this existence variable to the console log function to print the result on the terminal like:

 
Execute the program and observe the following result from the terminal:


As you can see, the result was false meaning that there is no such key as martialStatus inside our personObject.

Method 2: Using the “hasOwnProperty()” Method With the Object

In JavaScript, every object has some of the methods from its prototype. One such method is known as the hasOwnProperty(). This method takes in the key you want to search for in its argument and returns true or false depending upon the presence of the key in that object.

To demonstrate hasOwnProperty(), create an object using the following lines of code:

var car = {
  model: «2015»,
  make: «Porsche»,
  price: 328000,
  reviews: 4.8,
};

 
As you can already tell, the above lines are to create an object of a car. What we want to find is the presence of the key “make” in the object “car”. For this, apply the hasOwnProperty() method on the car object with the help of a dot operator and pass in the key “make” in its argument like:

existence = car.hasOwnProperty(«make»);

 
After that, simply pass the existence variable in the console log function to display the result on the terminal like:

 
Execute the program for the following outcome:


The output on the terminal is true, which means the car object contains the key make. After that, let’s check for the existence of the key “mileage” in our car object. For this, simply pass the key as mileage in the hasOwnProperty() method’s argument:

existence = car.hasOwnProperty(«mileage»);

 
To show the result on the terminal, simply pass the variable “existence” in the console log function:

 
Execute the program and observe the following output:


The output shows that there is no such key as mileage in the object car.

Conclusion

In JavaScript, we can quickly check the existence of a specific key inside an object with two different methods. The first methods include the use of the in operator, and it returns true if the existence is found otherwise, it returns false. The second method includes the use of a method of the JavaScript Object, which is the hasOwnProperty(). In its argument, you simply pass in the key you want to search for, and it returns true if the key is found in the object. Otherwise, it returns false.



Source link


If you have a laptop with a broken key then there is no need to worry because, in this guide, we will learn the method to fix the broken key on a laptop. We are going to discuss the convenient methods of fixing a broken key on a laptop.

What are the components of a key of the keyboard of the laptop

Every key of the keyboard has three components which are a keycap that is physically seen on the keyboard, the keypad, which is the mechanical button that communicates the signal to the system that this key has been pressed, and its key retainer is the support to hold the keypad and keycap.

How to fix loose keycap on the keyboard

The loose keycap is the issue that is mostly faced by the laptop users, if the keycap is loose, simply hold the keycap and slightly press it so it can be fixed. And if the keycap is split from the keyboard, then you should follow the steps mentioned below:

  • First shutdown the laptop
  • Then set the key retainer on the keypad of the laptop
  • Then insert the specific keypad in the key retainer and then cover it by putting it on the retainer and keypad
  • Press the keycap gently on the keyboard and it will be fixed

What happened if the key still does not work on the laptop

After setting the key cap, key retainer, and keypad, if the key is still not working then there will be a hardware issue that can only be resolved by changing the controller circuit of the keyboard. For this purpose, you can contact the manufacturer of the keyboard or can get help from the Authorized stores of that manufacturer.

How to fix the spacebar key on a laptop

The spacebar key is assembled differently from the other keys of the keyboard because of the size of the spacebar. It contains 2 or more 2 retainers that are present on the keyboard to hold and support the spacebar key. To fix the spacebar, a metal bar is present at the bottom of the spacebar which can be fixed with the retainers by placing it in alignment with it. If the metal bar is not present, then take a thin and strong metal wire and turn it into an L shape from both ends and place it on the retainers.

How to fix the key if it sometimes works and sometimes not

The partial working of the keys of the laptop means that there are dust particles around the keypad that are making hurdles to communicating the message of pressing and releasing keys. To fix it, remove the keycaps of the specific keys carefully and then remove the key retainer. With the help of earbuds, remove the dust particles around the keys and then place back the retainer to its position and then a keycap on it. The keys will work perfectly now.

Conclusion

The keys of a keyboard sometimes don’t work either because it is being isolated from the keyboard or due to dust particles. This guide will help you in fixing the issue with the keys of the keyboard and if it is still not helpful then it is recommended to contact the manufacturer of the keyboard.



Source link