0

I want to add a dimension to my multidimensional array called description. The objective is to go through the array, count the children and add a "parent" dimension based on the number of elements existing in description. But i'm currently stuck trying to append information into my array. I want to add this "parent" dimension, or at least be able to put a string into the location description[x]["TimeSeries"]["parent"]. Any help on how to proceed?

(function () {
 var description;
 description = {
     1: {
         source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
         TimeSeries: {
             //parent: "#hero-three",
             title: "Clients Installed"
         }
     },
     2: {
         source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
         TimeSeries: {
             //parent: '#g2-1'
         }
     },
     3: {
         source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
         TimeSeries: {
             //parent: 
         }
     }
 };

 $(description[3]["TimeSeries"]["parent"]).append('g2-2');

 var g = new Graphene;
 g.build(description);
 alert(description[3]["TimeSeries"]["parent"]);
 }).call(this);
5
  • Try to ask your doubt more clear. Commented Jun 13, 2013 at 8:17
  • Please format your code to make it more readable. Commented Jun 13, 2013 at 8:18
  • First of all, there is no array in your code - arrays are defined with [] not {}. Commented Jun 13, 2013 at 8:19
  • Okay, just tell us what you really want. From your code: you are trying to call method append(...) of nonexistent object lying under description[3]["TimeSeries"]["parent"]. In fact TimeSeries object doesn't have key parent yet. I assume you expect string 'g2-2' to be appended to an array lying under index parent. From your description: 'add a "parent" dimension based on the number of elements existing in description'. So tell us, what is the relation between 'g2-2' and the number of elements existing in description? Commented Jun 13, 2013 at 9:42
  • what i want to do is to be able to, for example, add content to description[3]["Timeseries"]["parent"]...or better yet, create parent under TimeSeries and assign a value to it. Commented Jun 13, 2013 at 9:59

3 Answers 3

1

There are a few things to note here; so hopefully this should help improve your approach to what you're trying to achieve, and also answer your question in the process.

The first thing to consider is that you're not using a multi-dimensional array, you're actually using an object. The syntax for an array is var myArray = []; whereas you're using var myObject = {};. This isn't necessarily a bad thing... objects are good!

From what I can gather, you've actually got an array of Description objects. If this is the case you'd want to construct it like so...

var descriptions = [
    {
        source: '...',
        timeSeries: {
            parent: '...',
            title: '...'
        }
    },
    {
        source: '...',
        timeSeries: {
            parent: '...',
            title: '...'
        }
    }];

You can then access your required information by doing the following:

for (var i = 0; i < descriptions.length; i++) {
    var current = descriptions[i];
    current.timeSeries.parent = '...';
}
Sign up to request clarification or add additional context in comments.

1 Comment

read this github.com/jondot/graphene - you have an example structure he needs to configure Graphene - your proposal is not accurate. I also pointed out a long time ago, that he is not using arrays, as he thought.
0

You need to walk through your object. jquery's _.each function can help you doing this:

var description = {
    1: {
        source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
        TimeSeries: {
            //parent: "#hero-three",
            title: "Clients Installed"
        }
    },
    2: {
        source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
        TimeSeries: {
            //parent: '#g2-1'
        }
    },
    3: {
        source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
        TimeSeries: {
         //parent: 
        }
    }
};

var parents = ["parent1", "parent2", "parent3"];

$.each(description, function (key, value) {
    value["TimeSeries"]["parent"] = parents.shift();
});

console.log(description);

http://jsfiddle.net/yJKr9/2/ or using underscore each: http://jsfiddle.net/yJKr9/

Comments

0

Instead of:

$(description[3]["TimeSeries"]["parent"]).append('g2-2');

Use:

description[3]["TimeSeries"]["parent"] = 'Whatever you want';

Using jQuery here is weird.

Also:

var g = new Graphene; //This is bad
var g = new Graphene(); //This is good

However, I haven't used Graphene, so I don't know if it requires some arguments for constructor. To test proper assignment of parent i removed Graphene related lines from my jsfiddle: http://jsfiddle.net/f9LaC/

7 Comments

That doesn't seem to work though. What the "parent" index does here is specify a place for a graph to be placed in my main html file..g2-2 refers to a location on that page.. i tried using jQuery i.e, use the extend function but that doesn't work either.. it's like i can't change values from outside the object..
This is not true, what you are saying - try problem is with initializing Graphene - see jsfiddle.net/f9LaC - removed two lines from your code.
Thanks.. there was a problem with the intitialization..also, can i use extend to add "parent"? if so, what's the syntax? Thanks!
Do you mean jQuery .extend(...) - no, this is applicable only to jQuery collections - using them in this context is weird, as I said. What would be the benefits of using extend here?
I'm no sure about your second part. I thought the new keyword automatically runs the function passed to it. I'm pretty sure new Graphene is correct. new Graphene() would only be correct if the Graphene function returned a function that was the actual constructor.
|

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.