1

I have one function in which I am iterating across data object which I have fetched from database. In the foreach loop I am trying to create one object(trigger) and pushing it to another variable(Geo) which I will use to put in another variable(triggers). Below is the code-

var Geo={};

array.forEach(this.cityData,lang.hitch(this, function(data,i){
    var trigger = {
        type: "Inside",

        event: {
            name: data.Name,
            address:data.Address
        }
    };

    var Location= "Location_"+i;
    Geo.Location=trigger;  // pushing trigger in Geo variable
}));

var triggers = {
    Geo  //using Geo in trigger
};

is var triggers={Geo}; equivalent to this below code ?

And is my pushing code Geo.Location=trigger; correct ?

var triggers = {
    Geo: {
        Location_1: trigger1,
        Location_2: trigger2 ...... and so on...
    }
};
4
  • hi possibly duplicate of this thread pls check out.. stackoverflow.com/questions/11078630/… Commented Sep 25, 2013 at 12:54
  • You question seems to want both objects to be inside eachother, which doesn't make much sense. Commented Sep 25, 2013 at 12:56
  • You code have exactly 0 JSON. Commented Sep 25, 2013 at 13:10
  • Yes Oleg....I shouldn't have used word JSON object....as these are only javascript variables with some properties. Thanks for pointing out this. Commented Sep 25, 2013 at 15:55

2 Answers 2

3

I didn't tested it but it looks like it does almost the same. Just one thing: This should give you an exception:

var triggers = {
      Geo  //using Geo in trigger
    };

The statement should be

 var triggers = {
      'Geo': Geo  //using Geo in trigger
    };

otherwise triggers will not have a Geo property.

Geo.Location=trigger; is just fine.

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

Comments

0

A property in an object accessed through dot notation (obj.property) is always considered simply as the propoerty name - i.e. variables are not evaluated. You can have dynamic property names by using the bracket notation: obj[property], which convert "property" to a string (resolving the variable value if necessary) and uses that as the actual property name.

So, try changin:

Geo.Location

to:

Geo[Location]

Edit: I'm not sure what is the expected final result, but if you want to achieve an object as shown in your last code block, the correct syntax should be:

triggers.Geo = Geo;

again, in consideration of the fact that the "Geo" in the dot notation form is simply the string name of the property, and has no relation to the variable of the same name.

2 Comments

not true, Geo.Location = x and Geo["Location"] = x does the same, it extends the Geo object with a property named Location. Geo[Location] = x extends the Geo object by an anonymous function returning x (in Chrome and it doesn't work in IE)
@Ela: i agree about your first affermation (Geo.Location = x and Geo["Location"] = x are equivalent), but i don't see any evidence about the correctness of the second one - Geo[Location] = x will add a property in the object having a name equal to the value of the variable Location (in the example by the op, this would cycle as "Location_1", "Location_2" and so on) - i made a quick fiddle to show what i mean: jsfiddle.net/TtJMn . Or maybe i misunderstood what you were meaning?

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.