ncubica
ncubica

Reputation: 8495

Best practice when object is undefined if - else

I would like to know if my code approaching is good, I would like to:

  1. Check if an object exist
  2. If not exist create it and assign it properties
  3. If already exist just assign properties

what I have now is the follow code, But I don't like to write two times the same line

function doSomething(_whatever){
    if(typeof someobject === "undefined"){
        someobject = { //dont exist
            profile : "some value",
            status : []
        }

        someobject.status.push(_whatever);
    }else{
        someobject.status.push(_whatever); //because already exist
    }
}

what is a better way to write this snippet? or do it in a better and less repetitive?

thanks in advance

------ original function

function addPerson(_person){
    var people = Iee.dashboard.analytics.data.people.data;      
    if(typeof people[_person.Id_Emp] === "undefined"){
        people[_person.Id_Emp] = {
            profile : _person,
            status : []
        }

        people[_person.Id_Emp].status.push({Id_Emp : _person.Id_Emp, status : _person.Estatus1, estatusby : _person.Centro_de_trabajo});
    }else{
        people[_person.Id_Emp].status.push({Id_Emp : _person.Id_Emp, status : _person.Estatus1, estatusby : _person.Centro_de_trabajo});
    }

    addBlackList(_person);
}

Upvotes: 3

Views: 652

Answers (3)

Dagg Nabbit
Dagg Nabbit

Reputation: 76766

In this case you expect it to be either an object or undefined, but not a string, a number, etc., so you can just check if it has a truthy value.

function addPerson(_person) {
    var people = Iee.dashboard.analytics.data.people.data,
        person = people[_person.Id_Emp];

    if (!person) person = {
        profile: _person,
        status: []
    };
    person.status.push({
        Id_Emp: _person.Id_Emp, 
        status: _person.Estatus1, 
        estatusby: _person.Centro_de_trabajo
    });

    addBlackList(_person);
}

This should perform slightly better than the redundant check mentioned in other answers, as it will only assign a value to the variable if a (truthy) value is not already present.

Just for fun, here's an ultra-condensed version:

function addPerson(_person) {
    var people = Iee.dashboard.analytics.data.people.data, id = _person.Id_Emp;

    (people[id] || { profile: _person, status: [] }).status.push({
        Id_Emp: id, status: _person.Estatus1, estatusby: _person.Centro_de_trabajo
    });
    addBlackList(_person);
}

Upvotes: 1

Anoop
Anoop

Reputation: 23208

Simplified code

function addPerson(_person){
        var people = Iee.dashboard.analytics.data.people.data;      
        people[_person.Id_Emp] = people[_person.Id_Emp] || {
                profile : _person,
                status : []
            };

            people[_person.Id_Emp].status.push({Id_Emp : _person.Id_Emp, status : _person.Estatus1, estatusby : _person.Centro_de_trabajo});

        addBlackList(_person);
    }

Upvotes: 4

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324760

A common way to do this is a redundant check:

someobject = someobject || {
    project:"some value",
    status:[]
};

Upvotes: 4

Related Questions