-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?

Ben K
  • 3
  • 6
  • 1
    Looking for something like [this](http://stackoverflow.com/questions/4329092/multi-dimensional-associative-arrays-in-javascript)? – pushkin Nov 05 '15 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. – mugabits Nov 05 '15 at 18:12

5 Answers5

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
Andrew Evt
  • 3,613
  • 1
  • 19
  • 35
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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TayTay
  • 6,882
  • 4
  • 44
  • 65
  • 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 Nov 05 '15 at 18:30
  • @Pushkin I've added your suggestion, please see edit. – TayTay Nov 05 '15 at 18:35
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'
 }, 
}
Dokinoki
  • 171
  • 2
  • 10
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"
    }
}
sunitj
  • 1,215
  • 1
  • 12
  • 21
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;
  }
}
dgmdan
  • 405
  • 6
  • 14