-4

i am a newbie to js. i was reading the definite guide and came through this, so i created a player object like this

   var player=
{
    name: 'the Player',
    age: 33,
    address:'street 32'
}

var addr = "";
for (i = 0; i < 4; i++) {
    addr += player["address" + i] + '\n';
}

Now the book says the for loop will add This code reads and concatenates the address0, address1, address2, and address3 properties of the player object. But it is not working..

Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • 1
    `And then i use the [] notation to add properties..` - that code isn't adding properties – Jaromanda X Oct 10 '15 at 04:43
  • Possible duplicate of [Iterate through object properties](http://stackoverflow.com/questions/8312459/iterate-through-object-properties) – Alvaro Silvino Oct 10 '15 at 04:44
  • but the book says it concatenates the properties,,.. Should i create a add property first? – Cloudboy22 Oct 10 '15 at 04:44
  • concatenate is not create – Jaromanda X Oct 10 '15 at 04:44
  • @Please tell me how do i fix that. i mean how i concatenate – Cloudboy22 Oct 10 '15 at 04:45
  • concatenate what?. what do you expect your code to produce, when you've figured that out, add it to the question – Jaromanda X Oct 10 '15 at 04:46
  • 1
    @MarcAndreJiacarrini it's not clear what you want to do with the code above... – Roko C. Buljan Oct 10 '15 at 04:46
  • @MarcAndreJiacarrini you have only one `player` Object. Than suddenly you loop 4 times *something* - and here's where i'm lost. – Roko C. Buljan Oct 10 '15 at 04:47
  • ok. i guess i have to go thorugh the chapter again,. – Cloudboy22 Oct 10 '15 at 04:48
  • Please edit your question to explain what you are actually trying to accomplish. Your current code is wrong and thus doesn't really make sense to us. So, instead of just giving us your flawed code with no problem explanation, please describe the actual problem you are trying to solve. – jfriend00 Oct 10 '15 at 04:49
  • @MarcAndreJiacarrini definitely. If you want to add one address to the `player`, than use `player.address = "Street 32";`. What you're doing is trying to access (get/read) inexistant properties `address0`, `address1`, `address2` etc – Roko C. Buljan Oct 10 '15 at 04:49

1 Answers1

0

To add only one address property to your player object do like

player.address = "Some Street 32";

If you want to add 4 "Address fields"/ properties to your player object:

var player={
    name: 'the Player',
    age: 33
};

for (i = 0; i < 4; i++) {
    player["address" + i] = "empty";
}


console.log( player );

[object Object] {
  address0: "empty",
  address1: "empty",
  address2: "empty",
  address3: "empty",
  age: 33,
  name: "the Player"
}

Though I see no use-case of pre-creating properties that you'll not use in the future. Rather create an Array of addresses (if you need to store more addresses) like:

player = {
  addresses = [],
  name = "the Player"
}

than whenever you need to add a new address to the Player do like:

player.addresses.push("Street 32");
player.addresses.push("Newstreet 43");

console.log( player.addresses );    // ["Street 32", "Newstreet 43"]
console.log( player.addresses[0] ); // "Street 32"

Than whenever you want to get all addresses you can loop the player.addresses or if you want to get only the newest address (the latest one in Array) you simply do:

var playerLatestAddress = player.addresses.slice(-1).pop(); // "MyNew, Street 43"

Instead your code currently does:

var addr = "";            // Create an empty string to concatenate to
for (i = 0; i < 4; i++) { // four times....
    // add to `addr` a value from an existent `player.addressN` property
    addr += player["address" + i] + '\n'; // ERROR: player.address0 is undefined
}

therefore if you test your console.log( addr ) you'll get:

"undefined
undefined
undefined
undefined
"

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313