const obj = {
"accountId": "number",
"prefix": "string",
"firstName": "string",
"middleName": "string",
"lastName": "string",
"suffix": "",
"dateOfBirth": "string",
"email": "string",
"phone": "string",
"status": ""
};
//JSON.stringify(obj); //assuming this does nothing since it's already a JSON string?
//JSON.parse(obj); // This kills the process.
const required = function(k) {
v = obj[key];
if (v == ''){
checked = "<br>A required component " + k + " was missing from this message.";
return checked;
} else {
checked = "<br>the value of " + k + " is " + v;
return checked;
}
}
for (var key of Object.keys(obj)) {
document.getElementById("demo").innerHTML = document.write(required(key));
};
I'm running the above in the w3 tryit(js) editor. the return is as follows
undefined
the value of accountId is number
the value of prefix is string
the value of firstName is string
the value of middleName is string
the value of lastName is string
A required component suffix was missing from this message.
the value of dateOfBirth is string
the value of email is string
the value of phone is string
A required component status was missing from this message.
I'm trying to understand why the first line of output comes up as undefined. in my use case this would throw a wrench in the works. looking through various answers I've tried forEach and get the same result. tried throwing in JSON.parse(obj) which seems to die. I've also added JSON.stringify(obj) which doesn't change the output.
I've combed through several times I'm fairly certain that I'm not missing something silly like a , or ;.
After that first line coming back as undefined everything is coming back as expected. I'm stuck.
document.writealways returnsundefined. Trydocument.getElementById("demo").innerHTML += required(key);instead.keyto get the object value, it should bk. v = obj[k] instead of. v = obj[key] jsfiddle.net/6ucjpbdm/1 Check above fiddle.