开发者

Creating nested arrays on the fly

开发者 https://www.devze.com 2022-12-23 16:03 出处:网络
I am trying to do is to loop this HTML and get a nested array of this HTML values that i want to grab.

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">
              开发者_Go百科    <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....


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


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');
    });
  });
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消