views:

88

answers:

2

I am trying to do is to loop this HTML and get a nested array of this HTML values that i want to grab.

It might look complex at first but is a simple question...

html

<div class="configureData">
               <div title="Large">
                  <a href="yellow" title="true" rel="$55.00" name="sku22828"></a>
                  <a href="green" title="true" rel="$55.00" name="sku224438"></a>
                  <a href="Blue" title="true" rel="$55.00" name="sku22222"></a>
                </div>
              <div title="Medium">
                  <a href="yellow" title="true" rel="$55.00" name="sku22828"></a>
                  <a href="green" title="true" rel="$55.00" name="sku224438"></a>
                  <a href="Blue" title="true" rel="$55.00" name="sku22222"></a>
                </div>
             <div title="Small">
                  <a href="yellow" title="true" rel="$55.00" name="sku22828"></a>
                  <a href="green" title="true" rel="$55.00" name="sku224438"></a>
                  <a href="Blue" title="true" rel="$55.00" name="sku22222"></a>
             </div>
   </div>

javascript

  //This script is just part of a Object containing methods.
parseData:function(dH){
        dH.find(".configureData div").each(function(indA, eleA){
                   colorNSize.tempSizeArray[indA] = [eleA.title,[],[],[]]
                     $(eleZ).find("a").each(function(indB, eleB){
                              colorNSize.tempSizeArray[indA][indB] = eleB.title
              })
            })
        },

I want the final array should look like this.

 [
   ["large", 
      ["yellow", "green", "blue"],
      ["true", "true", "true"],
      ["$55", "$55","$55"]
   ],
   ["Medium", 
      ["yellow", "green", "blue"],
      ["true", "true", "true"],
      ["$55", "$55","$55"]
   ]
 ]
// and so on....
+1  A: 

Something like this should work. (Your code wasn't too far off.)

parseData:function(dH){
  var results = [];
  dH.find(".configureData div").each(function(indA, eleA){
    var div = $(this);
    var result = [div.attr('title'),[],[],[]];
    results[results.length] = result;
    div.find("a").each(function(indB, eleB){ 
      var link = $(this);
      result[1][result[1].length] = link.attr('href');
      result[2][result[2].length] = link.attr('title');
      result[3][result[3].length] = link.attr('rel');
    });
  });
} 
John Fisher
John +1, Thanks
adardesign
John, I am still going to make a char count to see which solution is smaller in size and performance.Thanks.
adardesign
+2  A: 

Given your HTML and this jQuery snippet:

var result = [];
$('.configureData div').each(function () {
    var $a = $('a', this);
    result.push([this.title,
            $.map(['href', 'title', 'rel'], function (a) {
                return [$.map($a, function (v) {
                    return $(v).attr(a)
                })];
            })

    ]);
});

you get result set up the way you asked.

If you have Firebug, just do console.dir(result) and see.

Edit: I updated the script to extract arbitrary attributes into separate sub-arrays

Marko Dumic
Marko, Thanks, But how do i loop throgh the other ones?
adardesign
Never mind, I got it.Thanks much
adardesign
Let me edit my answer.
Marko Dumic
Sure go ahead :)
adardesign
Marko, can i ask you a question? is this the right approach or should i rather store the data in a object?I will later build new HTML out of this array's. Thanks
adardesign
I'd take different approach. I wouldn't keep attributes of the same object in different arrays.
Marko Dumic
Marko, Can you please clarify a bit further?
adardesign