0

Well, the problem is quite simple. I got an object of parsed table rows. Code for it is this:

var erg = [];
    $("tr").each(function (index) {
        var row = {};

        var test = $(this).children();
        row['column1'] = test[0].textContent;
        row['column2'] = test[1].textContent;
        row['column3'] = test[2].textContent;
        row['column4'] = test[3].textContent;
        row['column5'] = test[4].textContent;
        row['column6'] = test[5].textContent;
        row['column7'] = test[6].textContent;        
        erg.push(row); 
    });

And I wanna pass a variable var my_variable="blabla" to it without ruining the structure of the object. So how could i bring that object into a structure like this?:

Object{my_variable="my_variable_value"}, Object{my_table=[Object{...}, Object{...}]} //all the objects of the table

$.extend({}, erg, my_variable); only messed my object up.

I want it in that structure so i can pass it as json to my php script and filter my variable easily. Any tips, links, code snippets? :)

2 Answers 2

3

I'm not sure at which point you want to add that, but you may simply wrap your array with another object, and add your property to that same object.

This is basically what Florent's answer does, but using an object literal instead of a "class" and prototype:

// (your current code)
var wrapper = {
    my_variable: 'something',
    my_table: erg
};
Sign up to request clarification or add additional context in comments.

2 Comments

IMHO it's a cleaner approach than mine.
Hehe. Yeah I figured that out a few minutes ago. I feel so stupid right now :D It's really that simple.
1

You can define a class and add the needed variables to its prototype.
First you need a little utility to do that:

function createSharedStruct() {
  // Define a shared structure
  var Struct = function() {};

  // Define a method to define a shared variable
  Struct.share = function(variable, value) {
    Struct.prototype[variable] = value;
  };

  return Struct;
}

And then, update your code:

// Create the shared structure
var rowClass = createSharedStruct();

// Register your shared variables
rowClass.share('my_variable', 'my_variable_value');

var erg = [];
$("tr").each(function (index) {
    var test = $(this).children();

    // Create a new row
    var row = new rowClass();
    row['column1'] = test[0].textContent;
    row['column2'] = test[1].textContent;
    row['column3'] = test[2].textContent;
    row['column4'] = test[3].textContent;
    row['column5'] = test[4].textContent;
    row['column6'] = test[5].textContent;
    row['column7'] = test[6].textContent;        
    erg.push(row); 
});

// No matter when you share a variable, it will be defined among 
// all instances of the same struct.
rowClass.share('my_other_var', 42);

Now you can access shared variables:

console.log(erg[0].my_other_variable); // 42
console.log(erg[1].my_other_variable); // 42

Demo available on JSFiddle.

7 Comments

Wow! Thx for your answer! But I just was stupid and am not experienced in working with objects or jsons. There is a simple solution for my problem: var json = { table: erg, variable: my_variable };
Is there any valid reason why my answer was downvoted?
Perhaps because you overcomplicated the solution (it wasn't me, btw).
Oh I'm sorry. I voted your answer up because at the moment I was reading it it was the only one. But then I read the other, simplier solution and I upvoted that one too. But you can only upvote one answer, I didnt know that. But I think the other answer should appear first because it's the best and simpliest solution. Sorry :(
Well I do agree it is overcomplicated and I would have done what you suggested. I thought it was what the OP expected.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.