MrTacoBean
MrTacoBean

Reputation: 53

Handling new parameters for an Object

When it comes to handling new data for an object I'm not quite sure what may be the best method of handling this new data. I have two ways that I have figured it out myself but to me both definitely seem to have a code smell to them. Maybe there's a better way?

Below is an example of the situation:

// an array of people objects in which the objects should
// only contain specific info to that person
let people = [/* array of people */];

people.forEach(person => {
    /* Code to find the cellular carrier and phone number of that person */

    let carrier = result["carrier"];
    let phoneNumber = result["phoneNumber"];
});

Just to pause there this is where the problem starts. Carrier and phoneNumber are not identified in the person class/object. These are also parameters that I would not want to define explicitly in the person class even if they relate to the person. That info would only be used in this instance and I would not want to clutter the Person class.

Solution 1: Shoehorn the parameters into the existing object

person.carrier = carrier;
person.phoneNumber = phoneNumber;`

Solution 2: Create an entirely new object with the new parameters

let newPerson = {
    firstName: person.firstName,
    lastName: person.lastName,
    carrier: carrier,
    phoneNumber: phoneNumber
}

Either of these methods work for me but both seem dirty. I need this due to the fact that the people array might get iterated multiple times in different places to find different info. But that loop might rely on the person obj to have the carrier/phoneNumber parameters.

Upvotes: 3

Views: 70

Answers (1)

Michael Cacciano
Michael Cacciano

Reputation: 389

From what I gather when you loop through the people array which is full of person objects, you want to add some data to person without mutating the original person. I see you already are building a new object which is what I would do personally. There's a couple ways you could do it. You could build a another class that inherits from Person and create a new instance in the foreach of that, or you could also just do what you're doing with spread syntax.

const newPerson = {...person, param, param, etc}

This will give you everything from the original person and add the new properties. I believe you could also do something like

const newPerson = Object.create(person);
newPerson.carrier = carrier;
newPerson.phoneNumber = phoneNumber;

As a note es6 syntax there instead of {param: param} just {param} I'm still a bit new but I believe this should do what you're looking for.

Edited because I was using Object.create() wrong. This will create a new object that inherits the prototype of Person so it has access to the members. If you log person you will see the original person members, but if you log newPerson you should only see the added members. If you log newPerson.__proto__ you will see the inherited members

Upvotes: 3

Related Questions