Chichi Ukomadu Chichi Ukomadu - 2 years ago 76
Javascript Question

javascript function that takes two inputs: an object and a key and returns the corresponding value for that key within the object

I'm trying to create a JavaScript function that takes an object as input, and returns the values of all the keys in the object. Please kindly help review my code, I'm not getting the result I expect.

var james = {
firstName: “James”,
lastName: “John”,
age: 40,
job: “programmer”,
married: false

function hasElObject(valObject, key) {
var i = valObject.length;
while (i--) {
if (valObject[i] === key) {
return valObject[i];
return ‘key not found’;
var objectEl = hasElObject (['congratulations', 'top', 8, 90], 5);
log (arrayEl);

var retValue = getValue(mary,email);
log (retValue);

Answer Source
function hasElObject(valObject, key) {
  for (var k in valObject) {
    if (k == key)
      return valObject[k];
  return 'key not found';

var jobOfJames = hasElObject(james, "job");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download