2

I can't believe this question would not have been asked before, but I searched as much as I could and couldn't find it so here it goes.

I want to test if a dynamically built JavaScript object exists. However when I test this:

var myObject = {};
var dynamicName = "customName";

if(myObject[dynamicName].object == undefined){
  myObject[dynamicName].object = "something"; // Make an entry in the dynamic object
  alert("The object didn't exist, so we populated it");
}else{
  alert("The object already exist");
}

If the object doesn't exist and I try to run the above code I get an error saying "myObject[dynamicName] is undefined" and all javascript execution halts.

Is there a way to check if this object exists I want without causing such an error?

Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
YAHsaves
  • 1,697
  • 12
  • 33
  • Possible duplicate of [Check if object exists in JavaScript](https://stackoverflow.com/questions/4186906/check-if-object-exists-in-javascript) – Joseph Cho Jul 26 '18 at 02:31
  • Because that alert saying it's not defined is in the same if that's defining it. So the order of the if is: 1. test and find not defined, 2. define it, 3.alert (not defined) – Mark Jul 26 '18 at 02:31
  • @MarkMeyer How would that explain the error? I understand the alerts aren't perfect but that's just psudeo code. I really want to get rid of the error – YAHsaves Jul 26 '18 at 02:32
  • I don't get an error when I run your code (other than the syntax errors), but I did make a `myObject` object and populate `dynamicName` with a string. It would help if you made a runnable snippet with the (<>) button that showed the error. – Mark Jul 26 '18 at 02:33
  • @MarkMeyer Sorry I posted this question too quick without testing it myself, I edited the code to reflect more my true circumstances and this does cause the error – YAHsaves Jul 26 '18 at 02:36
  • ...ah that's much different. – Mark Jul 26 '18 at 02:37
  • Sample input please – Isaac Jul 26 '18 at 02:41

3 Answers3

2

If myObject[dynamicName] doesn't exist, you can't access its properties. Trying to access or assign a property of undefined causes an error.

You need to create the object, then you can create the property

if (myObject && myObject[dynamicName] && myObject[dynamicName].object !== undefined) {
    alert("The object already exists");
} else {
    myObject = myObject || {};
    myObject[dynamicName] = myObject[dynamicName] || {};
    myObject[dynamicName].object = something;
    alert("The object didn't exist, so we populated it");
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you that worked! Just to make sure I understand what's going on, are we creating the object piece by piece as we go along? – YAHsaves Jul 26 '18 at 02:47
  • Yes, that's what it's doing. It uses `||` so if that level of the object nesting already exists, it's reused. – Barmar Jul 26 '18 at 14:41
1

There is a shorter version, (almost) without clumsy if's

const myObject = {};
const dynamicName = 'customName';

const { [dynamicName]: obj = {} } = myObject;
if (obj.object == null) {
  obj.object = 'something'
  console.log('populated');
} else {
  console.log('existed');
}
myObject[dynamicName] = obj;

console.log(myObject)
amankkg
  • 4,503
  • 1
  • 19
  • 30
0

Check typeof == object

var userObject={
"name":"abc",
"kyc":true
};

var dynamicName="name";

if (typeof userObject == 'object' && userObject[dynamicName] != undefined) {
  console.log("UserObject Exists and valid");
}else{
console.log("UserObject does not Exists");
}
Dinesh Ghule
  • 3,423
  • 4
  • 19
  • 39