Creating a JavaScript Object as Name Value Pairs

I thought describing how I solved an issue today might help someone else.

I’m working with JqGrid and needed to add a new row full of empty strings. I’ll actually make this more complex later based on the column attributes, but the simple version makes the point.

The documentation for adding a new row is:

clip_image001

Problem is that I’m creating empty data based on the columns – I don’t know what the columns are ahead of time.

I leveraged the fact that JavaScript objects are named value pairs for their properties. The code above creates an object with the properties defined, but you can also create an object with a dictionary style syntax:

clip_image002

This code returns an object which has properties for each column in the grid. Each column contains an empty string, except the key column.

With JQuery causing a resurgence in JavaScript, I suspect the different thinking required to solve a problem like this is going to trip up plenty of .NET coders.

2 thoughts on “Creating a JavaScript Object as Name Value Pairs”

  1. Kathleen, if you are digging into Javascript, I highly recommend reading Douglas Crockford’s “Javascript: The Good Parts” Also, JetBrains’ WebStorm is a fantastic IDE for working with Javascript. Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>