0

I'm trying to create a script that will insert some values in to an object.

I basically want to end up with a series of objects that would look something like this -

myObj.data.person[0].name = 'name 1';
myObj.data.person[1].name = 'name 2';
myObj.data.person[2].name = 'name 3';

etc

Here is my object which contains an array of objects.

var addressBook = {
  data: [ 
    person = {
      name: '',
      address: ''
    }
  ]
}

And a for loop to insert repeating information.

for (i=0; i < 10; i++) 
  {
    myObj.data.person[i] = {name: 'Joe Bloggs', address: 'Main Street'};
    console.log(myObj.data.person.name);
  }

Whenever I run this code I get the following error -

Uncaught TypeError: Cannot set property 'person' of undefined at <anonymous>:14:24

So, the question is where am I going wrong ? And furthermore would this be considered the right way to go about creating a list of Objects (e.g. Person 1, Person 2 etc)?

(I'm ultimately thinking of how I can create something like a Person constructor and use a loop to create multiple Person objects).

Thanks,

3
  • You need to declare your myObj property correctly, Commented Feb 14, 2017 at 11:41
  • you declare your object as addressBook, but later try to access it as myObj? Commented Feb 14, 2017 at 11:41
  • data: [ person = ... ] is nonsense. If data is an array, then you cannot do data.person[...] either. You probably just want data: [ { name ... } ], and manipulate data[i]. Commented Feb 14, 2017 at 11:42

3 Answers 3

1

Please try to change your object notation in following way

var addressBook = {
  data: [{
      name: '',
      address: ''
    },
    {
      name: 'Roshan',
      address: ''
    },
    {
      name: 'Roshan1',
      address: ''
    }
  ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's half the answer…
Thanks Isetty and Deceze. This was the right solution.
0

try this :

myObj.data[i].person = 'John';
myObj.data[i].address = 'NYC';

Comments

0

See this Answer as addendum to isetty ravitejakumar's Answer

You should consider writing a prototype / class for your usecase. In this case you could keep better track of your data.

maybe something like this:

var AddressBook = {},
    Person = {};

(function() {
    AddressBook.prototype = {
        data: [],
        add: function(person) {
            this.data.push(person);
        }
    };
    Person = function(name, address) {
        this.name = name;
        this.address = address;
    }
    Person.prototype = {
        name,
        address
    };
})();

1 Comment

Thanks jayjay - that's also really useful to know. Beyond the problems I asked about in the original question - this is what I was ultimately trying to setup. Cheers.

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.