Record Collection in JavaScript

Posted by

Tutorial on Record Collection in JavaScript

Introduction

A record collection is a data structure that stores a collection of records. Records are key-value pairs that are typically used to store information about objects, such as a person’s name, address, age, etc. In JavaScript, record collections can be implemented using objects.

Creating a Record Collection

To create a record collection, you need to create an object that will store the records. This can be done by using the Object.create() method.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var recordCollection = Object.create(null);

[/dm_code_snippet]

Adding Records to the Collection

Once you have created the record collection, you can start adding records to it. This is done using the Object.assign() method. This method takes two parameters: an object to be assigned and a key-value pair that will be assigned to the object.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Object.assign(recordCollection, {
  name: 'John Doe',
  age: 35,
  address: '123 Main Street'
});

[/dm_code_snippet]

Accessing Records in the Collection

Once you have added records to the collection, you can access them using the Object.getOwnProperty() method. This method takes two parameters: an object and a property name. It will return the value of the property if it exists in the object.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var name = Object.getOwnProperty(recordCollection, 'name');
// name will be 'John Doe'

var age = Object.getOwnProperty(recordCollection, 'age');
// age will be 35

var address = Object.getOwnProperty(recordCollection, 'address');
// address will be '123 Main Street'

[/dm_code_snippet]

Updating Records in the Collection

If you want to update an existing record in the collection, you can use the Object.defineProperty() method. This method takes three parameters: an object, a property name, and a value. It will update the value of the property if it exists in the object.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Object.defineProperty(recordCollection, 'name', 'Jane Doe');
// recordCollection.name will now be 'Jane Doe'

Object.defineProperty(recordCollection, 'age', 36);
// recordCollection.age will now be 36

Object.defineProperty(recordCollection, 'address', '456 Main Street');
// recordCollection.address will now be '456 Main Street'

[/dm_code_snippet]

Deleting Records in the Collection

To delete a record from the collection, you can use the Object.deleteProperty() method. This method takes two parameters: an object and a property name. It will delete the property if it exists in the object.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Object.deleteProperty(recordCollection, 'name');
// recordCollection.name will now be undefined

Object.deleteProperty(recordCollection, 'age');
// recordCollection.age will now be undefined

Object.deleteProperty(recordCollection, 'address');
// recordCollection.address will now be undefined

[/dm_code_snippet]

Iterating over Records in the Collection

If you want to iterate over all the records in the collection, you can use the Object.keys() method. This method takes an object and will return an array of all the property names in the object. You can then use this array to iterate over the records in the collection.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var records = Object.keys(recordCollection);
records.forEach(function(record) {
  console.log(recordCollection[record]);
});

[/dm_code_snippet]

Conclusion

In this tutorial, we have learned how to create, access, update, and delete records in a record collection in JavaScript. We have also seen how to iterate over the records in the collection. With these techniques, you can easily work with record collections in your JavaScript applications.