0

I'm trying to create an object that is updated dynamically.

Here's the setup of the type of object I'd like to create (note: I may add other things, such as address, country, etc to the keys):

var contacts = {"Bruce Wayne":{"phone number":'123-456-7890', "email":"[email protected]"}, "Alfred":{"phone number" :'987-654-3210', "email": "[email protected]"}, "Clark Kent":{"phone number":'951-753-8520', "email":"[email protected]"}}

So for each name (Bruce Wayne, Alfred, ...) I have some keys assigned to them.

I'm using npm faker to generate some fake data to try and populate an array like the above, with the outline

I'm able to get a loop going, but it always returns the last iteration's data. I understand it's because I'm doing contact = .... Since this is an object, I can't use push, AFAIK.

function getContact(numContacts){
    contacts = {}
    for (var i = 0; i < numContacts; i++){
        console.log(i);
        var name = faker.name.firstName() + " " + faker.name.lastName();
        var phoneNum = faker.phone.phoneNumber();
        var email = faker.internet.email();
        contacts = {name :{ "phone number": phoneNum, "email": email}}
        // contacts.name = {"phone number": phoneNum, "email":email};  // this also returns just the last instance.
    };
    return contacts;
};

var contacts = getContact(10); // This should create ten people, each with a phone number and email.

The loop almost successfully creates a single name. This returns:

name, 761.704.3328 x4287, [email protected]

But in that iteration, name variable is actually Joe Schmoe, not literally name...

What am I overlooking to make sure that the contacts object gets populated with 10 people, with the resolved name, not just the last in the iteration?

1
  • 1
    To add new property to your object, you need to do contacts[somePropertyName] = {...}. Doing contacts = {...} just replaces everything that was previous in the contacts object with a whole new object that only has the latest stuff in it. All previous contents are no longer there in the new object. Commented Jan 25, 2018 at 0:19

2 Answers 2

2

Observations

  • You're trying to use name variable as key, however, what you're doing is adding a key literally called name.

  • What you have to do, is to create the key programmatically as follow: contacts[name] and assign the object with phoneNumber and Email.

  • This code is an example to simulate your scenario.

var faker = {
  name: {
    firstName: function() {
      return "Clark";
    },
    lastName: function() {
      return "Kent";
    }
  },
  phone: {
    phoneNumber: function() {
      return '951-753-8520';
    }
  },
  internet: {
    "email": function() {
      return "[email protected]";
    }
  }
};

function getContact(numContacts) {
  var contacts = {}
  for (var i = 0; i < numContacts; i++) {
    var name = faker.name.firstName() + " " + faker.name.lastName();
    var phoneNum = faker.phone.phoneNumber();
    var email = faker.internet.email();

    contacts[name + '_' + i] = {
      "phone number": phoneNum,
      "email": email
    }
  }

  return contacts;
}

var contacts = getContact(10);

console.log(contacts);

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

3 Comments

All the faker stuff is taken care of by the npm package. I'm learning JavaScript so to me it looks like the main solution you have is the same as the one I selected. Am I missing something?
Basically, the same answer. However, I've put some observations to guide you. :-)
excellent, that's what I thought! I appreciate it, it gives me insight on how to set up my own thing like that in the future. :D
1

The names are the keys in your object. You can use it like an array index to populate contacts. This should work:

contacts[name] = {"phone number": phoneNum, "email": email}

1 Comment

Man, I struggled so much with this earlier, and I swear I tried that, but must have done it wrong at the time. This works, and seems like the easiest/best way. Thanks!

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.