0
var person1 = {
    firstName : "John",
    lastName  : "Doe",
    age       : 50,
    eyeColor  : "blue"
};

lot of object are there like above. i have tried get the object like below but not working.

var id = "1";

person+id.firstName + " is " + person+id.age + " years old.";

please guide me.

5
  • Its not very clear what you are asking, can you explain in more detail? Commented May 6, 2016 at 9:54
  • Very bad duplicated refferences. I had an example just for your case, but now it's impossible to answer. Commented May 6, 2016 at 9:55
  • i have javascript objects like person1, person2 ... each object some properties are there Commented May 6, 2016 at 9:56
  • get the object name dynamically with concat with number like person+1.fistname but result not came wihtout concat result will coming Commented May 6, 2016 at 9:57
  • person1.firstName + " is " + person1.age + " years old."; it is working fine. my requirement is var id = "1"; person+id.firstName; person+id.age; it is not working Commented May 6, 2016 at 9:59

1 Answer 1

1

If it's in global context then you can get it from window object

var person1 = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};

var id = "1";

document.write(window["person" + id].firstName + " is " + window["person" + id].age + " years old.");

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

1 Comment

@PrasadBezawada : glad to help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.