Home > JavaScript, programming, web > Javascript Arrays vs Object Literal

Javascript Arrays vs Object Literal

June 28th, 2007

Recently, I’ve been learning to use Javascript object literals for holding similar sets of data as opposed to using arrays. They are much more manageable and flexible than simpler than arrays and I think even easier to read. Below is an a variable holding form validation data. I can simply loop through these just like I would an array, I can output the values, check against them and even call a function or even set an event listener. With arrays I am limited to mostly common data types like string, int, boolean and such. The fact that I can make references to functions is really cool and allows a pretty flexible and powerful system. Next time you have to work with arrays, consider object literal.

var emptyValues = [{name:'firstname', id:'firstname', ce:checkEmpty, eid:'first_error', defVal: 'First'},{name:'lastname', id:'lastname', ce:checkEmpty, eid:'last_error', defVal: 'Last'},{name:'phone', id:'phone', ce:checkEmpty, eid:'phone_error', defVal:'Phone'},{name:'message', id:'message', ce:checkEmpty, eid:'message_error', defVal: 'Message'},{name:'name', id:'name', ce:checkEmpty, eid:'name_error', defVal: 'Full Name'},{name:'email', id:'email', ce:checkEmpty, eid:'error_email', defVal:'Email'}];

Rich JavaScript, programming, web

 
close Reblog this comment
blog comments powered by Disqus