-1

Hello I am very new to JavaScript and need some guidance. I am trying to build a reference for people in a club. I started by creating an array like so:

var People = ['Adam', 'Bruce', 'Steve']

But now I want to add characteristics to Adam, for instance height, weight, age, etc.

I want to be able to access information regarding people in my array by something like:

alert(People.Adam.height);

How would I structure it so that objects in my array has unique characteristics?

2
  • 1
    Looking for something like this? Commented Nov 5, 2015 at 18:10
  • change your People array to a People object, and then add the properties you want (name, height, weight, age, etc.) to the new People object. Commented Nov 5, 2015 at 18:12

5 Answers 5

2
var people = [],
    adam = {
        height: 200,
        weight: 200,
        age: 20
    }; 

people.push(adam);

console.log(people[0].height); // 200

or use object instead of array:

var people = {},
    adam = {
        height: 200,
        weight: 200,
        age: 20
    };

people.adam = adam;

console.log(people.adam.height); // 200
Sign up to request clarification or add additional context in comments.

Comments

1

You're currently adding strings into your array, not objects. You need to make your people into objects.

var adam = {
    name: 'Adam',
    height: 6.0
}

To retrieve Adam's height now, you'd call adam.height. So if you had an array, People, with adam (and others) inside, here's how you could do it:

var people = [adam]
alert(people[0].height)
// Alerts 6

Edit: alternatively, if you'd like to access Adam by name, you could make people an object instead of an array:

var people = {'adam' : adam}
alert(people.adam.height)
// Alerts 6

2 Comments

The only problem here is that OP wants to be able to access Adam by name people.adam.height. Here, you would need to know where adam was in the array. Of course you could loop through it and find the object with the right name, but I would suggest using an associative array instead.
@Pushkin I've added your suggestion, please see edit.
1

You can create an object, so you can access any nested property of that object:

var People = {
  Adam: {
    height: '187',
    age: '22',
    favorite_color: 'orange'
 },
 Paul: {
   height: '156',
   age: '38',
   favorite_color: 'blue'
 }, 
}

Comments

1

You need to make an object that have name of the people as key and the value for each key would be details for the person in form of an object. It could be something like

var People = {
    "Adam" : {
          "height" : "someValue",
         "anotherParam" : "someOtherValue"
    },
    "Bruce" : {
         "height" : "someValue",
         "anotherParam" : "someOtherValue"
    }
}

Comments

0

Use a class. It might look like this:

class Person {
  constructor(height, weight, birth_year) {
    this.height = height;
    this.weight = weight;
    this.birth_year = birth_year;
  }
}

Comments

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.