52

I am creating my own object:

gridObject = new Object();

I am then using jquery to pull the contents of list item tags, which themselves are filled with

tags that have specific class names:

<li row="1"><p class="department" rowitem="department">Photography</p>...</li>

I am pulling them using this code:

//make object from results
gridObject = new Object();
        
//get all the rows
var rowlist = $('li[row]');
        
for(var r=0; r<rowlist.length; r++) {
            
    //make gridObject row element here
            
    //get the row content
    var thisrow = $(rowlist[r]).html();

    //get all the p tags
    var rowitems = $(thisrow + 'p.[rowitem]');
            
    //get field name
    for(var ri=0; ri<rowitems.length; ri++) {
    if (r < 2) { //this is temporary just for testing
         var fieldname = $(rowitems[ri]).attr('rowitem');
         var fieldvalue = $(rowitems[ri]).html();
    }
    }

Ia m getting hung up passing this into my object. Two questions. Can an object property be made with a variable name, like so

griObject.fieldname = fieldvalue;

and can the objects have parent/child relationships such as:

gridObject.r.fieldname = fieldvalue; 

in this case both r and fieldname would be variables. Or should I just be working associative arrays to achieve something similar?

This is in answer to a follow up question I posted below: "Is there a print_r equivalent in javascript" - you can use iterator, a bit more typing but does the trick:

//loop through search data
var it = Iterator(filteritems); 
for(var pair in it) { 
    console.log("key:" + pair[0] + ", value:" + pair[1] + "\n");
}
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • 1
    In JavaScript an object *is* an associative array. You can index an object like `obj.prop` or `obj['prop']` - they're the same (except that the subscript way is more flexible on the property name). – Skilldrick Jan 14 '11 at 18:43
  • @Skilldrick I prefer the term "Map" as the use of "Array" in "Associate Array" makes some people think of PHP-style "arrays" and ordering, which is not accurate but is a common misconception... –  Jun 15 '12 at 00:15
  • 1
    **I voted to reopen this question** because it deals with **referring** to object properties using variables; not the creation of such object properties. – Serge Stroobandt Apr 02 '21 at 12:01

1 Answers1

93

If you want to use a variable property name, use subscript syntax:

var fieldname = 'test';

//These two lines are equivalent as long as fieldname is 'test':
gridObject[fieldname] = fieldvalue;
gridObject.test = fieldvalue
Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • Thanks Skilldrick! Didn't know about the assoc. array/object relationship...can't we all just standardize names of stuff...like in cocoa it's a dictionary, took me a week to realize a dictionary is just an associative array... : D – PruitIgoe Jan 14 '11 at 18:56
  • One other question - is there a similar command like PHP's print_r() that allows you to see all the key/value relationships of an object? – PruitIgoe Jan 14 '11 at 19:08
  • For the question above iterator is what I was looking for - see edit to original question for code. – PruitIgoe Jan 14 '11 at 19:38
  • @PruitIgoe If you're using Chrome or Firefox with Firebug you can use console.log() to print stuff to the console. Can't remember exactly how it does it in Firebug, but in Chrome that gives you an object that you can click on to inspect its members and sub-member. Really useful. – Skilldrick Jan 14 '11 at 20:12