Traditionally (and throughout most of my scripts up to this point) I have extracted data from an array by calling its position only and storing the value in a variable such as this:
var data = [surname, postcode, shoeSize];This works perfectly except if you scale it up and have an array with 10+ items for instance and you are not necessarily extracting them in a sequential list. By using name:value pairs however what we can do is assign a label (name) to the specific piece of data (value) within an array:
var surname = data[0];
var postcode= data[1];
var shoeSize= data[2];
var data = {surname:surname, postcode:postcode, shoeSize:shoeSize};So rather than calling the position of the value in the array we can now call its name, which will help to reduce errors when dealing with large arrays. This has the added benefit of your code being somewhat easier to understand by others as they can deduce more quickly what data you are calling. The trade-off is that a little bit more thought needs to go into creating the name:value pairs in the first instance.
var surname = studentData['surname'];
var postcode = studentData['postcode'];
var shoeSize = studentData['shoeSize'];
Below is some sample code for you to test this method out.
name:value pairs javascript.xlsx
No comments:
Post a Comment