私のコードが近づいているかどうかを知りたいのですが、次のことをしたいです:
- オブジェクトが存在するかどうかを確認する
- 存在しない場合は、作成してプロパティを割り当てます
- すでに存在する場合は、プロパティを割り当てるだけです
私が今持っているのは次のコードですが、同じ行を2回書くのは好きではありません
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
}
}
このスニペットを書くより良い方法は何ですか? それとも、より良く、より少ない反復でそれを行いますか?
前もって感謝します
------ オリジナル機能
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);
}