0

I'm trying to build an array of array in jquery to store data and use them later in my code. I'm looking to have something like this:

array[html_node][attribute] = attribute value

based on this type of DOM:

<div id= node>
  <a step= "downloading" data="123">
    ...
  </a>
  <a step= "waiting" data="122">
    ...
  </a>
</div>

can you help me figure this out?

4
  • 1
    Are you trying to store every single node from the DOM in an array? What exactly are you trying to achieve here? Commented Jul 7, 2014 at 19:10
  • 1
    What are you going to do with this? jQuery already can search and collect attribute data across the DOM for you. Even plain ol' js can do quite a bit of that with the getElement... functions. Commented Jul 7, 2014 at 19:11
  • 2
    you've already got the DOM. why duplicate it? Commented Jul 7, 2014 at 19:12
  • Can you show us what you would like your array values to look like? ie array[a][data] = 123? or array[downloading][data] = 123? Commented Jul 7, 2014 at 20:46

2 Answers 2

1

Although I agree with the previous comments (why create an array of stored data when the DOM is a perfectly good substitute?), here is something along-the-lines of what I believe you're after:

var arr = [];

$('#node a').each(function(){

  arr.push( $(this).attr('data') );

});

Or perhaps you are wanting the node as a class, where there will be multiple nodes:

var arr = [];

$('.node').each(function(i){

  arr.push([]);

  $(this).children('a').each(function(){

    arr[i].push( $(this).attr('data') );

  });
});

Or if you are wanting to store a reference to the node (which is difficult using just arrays as you'll need a key in there somewhere):

var arr = [];

$('.node').each(function(i){

  arr.push({ node:this, data:[] });

  $(this).children('a').each(function(){

    arr[i].data.push( $(this).attr('data') );

  });
});

Here's a fiddle (with references to the id of the node so-as not to choke the JSON.stringify part.

Sign up to request clarification or add additional context in comments.

7 Comments

NB: this is just an array of the data - there's no reference to which element actually held that data.
@Alnitak Very true, but the OP never said he wanted to reference the element; he only said he wanted to use later the data from the element. (I know, perhaps the OP expressed itself wrong, but so far this answer the question)
@chris-l I disagree - the OP clearly shows (albeit incorrectly) the intent to maintain a 2D array of element and attribute.
Would stringify be useful in this instance?
@PseudoNym01 Unfortunately not because the node-referencing would probably send it loopy
|
0

One of the hidden gems in Javascript which I think fades most people are the use of data structures. In particular the use of structs

For what you are trying to do, it is virtually impossible to have the layout like you want it

array[html_node][attribute] = attribute value

The reason being, you would have to assign numbers to your html_node's so you would either have to hardcode those in, in order to access them the way you want to, or create some complicated algorithm to retrieve the value of that specific node.

Another issue I am seeing here, is that there can be duplicate html nodes. Meaning, you can have multiple html_node values of 'DIV' but they have different attribute data. In any event, there can be only one object in the array for array[DIV] so there will also need to be some sort of way to distinguish between each node.


Your solution :

Javascript Structs

These have the ability to be super flexible as well as easily accessible.

First - You will need to make a struct factory to initialize everything. Don't worry if you don't completely understand the factory, all you need to do is copy and paste the Javascript code. (However, it is additionally helpful if you can grasp the concept)

function makeStruct(names) {
  var names = names.split(' ');
  var count = names.length;
  function constructor() {
    for (var i = 0; i < count; i++) {
      this[names[i]] = arguments[i];
    }
  }
  return constructor;
}

Second - Create your struct and initialize array

var HTML_Node = makeStruct("node step data");
var array = [];

Third - Add elements to your struct

$('#node').children().each(function(){
    array.push(new HTML_Node(this.nodeName, $(this).attr('step'), $(this).attr('data')));
});

Fourth - Get elements from your struct, which is real simple with a loop.

for(var i = 0; i < array.length; i++){
//All of these if statements will be referencing node / step / data as set up in step 2

    if(array[i].node == 'DIV'){
        //if the node is a DIV element do something
    }

    if(array[i].step == 'waiting'){
        //if the node's step is waiting do something
    }

    if(array[i].data == '123'){
        //if the node's data is 123 do something
    }
}

And that is how you can use structs to create a simple data structure to store all of the information needed.

2 Comments

nothing wrong with this, but I wouldn't go and overrride the globally available Node object...
@shennan good point, was just doing it for clarity, but I'll change it up. Thanks for pointing it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.