Prasanta Banerjee
Prasanta Banerjee

Reputation: 71

Javascript: Unable to retrieve values in an object which has function

Using Javascript, I wrote this code to create an object:

let employee = {
    emp_firstname: "Prasanta",
    emp_lastname: "Banerjee",
    emp_fullname: function(){
        return (this.emp_firstname + " " + this.emp_lastname);
    },
    emp_id: 673630,
    emp_horizontal:"QEA",
    emp_vertical: "Insurance",
    joining_date: "22/12/2017",
    emp_salary : 13579,
    emp_bonus : function(){
        return (this.emp_salary*1);
    }
};

Now, i'm interested in printing each property & its value so i wrote this code:

for (let eachEle in employee){
    if(typeof eachEle=='string' || typeof eachEle=='number'){
        console.log(eachEle + ":" + employee[eachEle]);
    }
    else if(typeof eachEle=='function'){
        console.log(eachEle + ":" + employee.eachEle());
    }
}

But, on executing, it works fine except for "emp_fullname" & "emp_bonus". Instead of showing the value, it shows me the function:

let employee = {
  emp_firstname: "Prasanta",
  emp_lastname: "Banerjee",
  emp_fullname: function() {
    return (this.emp_firstname + " " + this.emp_lastname);
  },
  emp_id: 673630,
  emp_horizontal: "QEA",
  emp_vertical: "Insurance",
  joining_date: "22/12/2017",
  emp_salary: 13579,
  emp_bonus: function() {
    return (this.emp_salary * 1);
  }
};

for (let eachEle in employee) {
  if (typeof eachEle == 'string' || typeof eachEle == 'number') {
    console.log(eachEle + ":" + employee[eachEle]);
  } else if (typeof eachEle == 'function') {
    console.log(eachEle + ":" + employee.eachEle());
  }
}

How am I supposed to retrieve the value for those two properties? I'm looking for answers using which I can modify the for...in loop & retrieve the value.

Upvotes: 3

Views: 65

Answers (5)

Ziv Ben-Or
Ziv Ben-Or

Reputation: 1194

Your mistakes are: 1. You wrote: typeof eachEle insted of: typeof employee[eachEle]: 2. The execute is: employee.eachEle() insted of employee[eachEle](). eachEle is a string.

let employee = {
    emp_firstname: "Prasanta",
    emp_lastname: "Banerjee",
    emp_fullname: function(){
        return (this.emp_firstname + " " + this.emp_lastname);
    },
    emp_id: 673630,
    emp_horizontal:"QEA",
    emp_vertical: "Insurance",
    joining_date: "22/12/2017",
    emp_salary : 13579,
    emp_bonus : function(){
        return (this.emp_salary*1);
    }
};

    for (let eachEle in employee){debugger
        if(typeof employee[eachEle]=='string' || typeof employee[eachEle]=='number'){
            console.log(eachEle + ":" + employee[eachEle]);
        }
        else if(typeof employee[eachEle]=='function'){
            console.log(eachEle + ":" + employee[eachEle]());
        }
    }

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 281736

Two things you need to change

  1. You need to check for the value of element for string, number and function and not the key

  2. While executing the function you need to use the brackets notation since its a dynamic key

let employee = {
    emp_firstname: "Prasanta",
    emp_lastname: "Banerjee",
    emp_fullname: function(){
        return (this.emp_firstname + " " + this.emp_lastname);
    },
    emp_id: 673630,
    emp_horizontal:"QEA",
    emp_vertical: "Insurance",
    joining_date: "22/12/2017",
    emp_salary : 13579,
    emp_bonus : function(){
        return (this.emp_salary*1);
    }
};

for (let key in employee){
    let eachEle = employee[key];
    if(typeof eachEle=='string' || typeof eachEle=='number'){
        console.log(key + ":" + employee[key]);
    }
    else if(typeof eachEle=='function'){
        console.log(key + ":" + employee[key]());
    }
}

Upvotes: 1

Shub
Shub

Reputation: 2704

You need to check the type of value, eachEle is value of key which for your object is always string.

let employee = {
  emp_firstname: "Prasanta",
  emp_lastname: "Banerjee",
  emp_fullname: function() {
    return (this.emp_firstname + " " + this.emp_lastname);
  },
  emp_id: 673630,
  emp_horizontal: "QEA",
  emp_vertical: "Insurance",
  joining_date: "22/12/2017",
  emp_salary: 13579,
  emp_bonus: function() {
    return (this.emp_salary * 1);
  }
};

for (let eachEle in employee) {
  if (typeof employee[eachEle] == 'string' || typeof employee[eachEle] == 'number') {
    console.log(eachEle + ":" + employee[eachEle]);
  } else if (typeof employee[eachEle] == 'function') {
    console.log(eachEle + ":" + employee[eachEle]());
  }
}

Upvotes: 1

CerebralFart
CerebralFart

Reputation: 3490

In your for loop, you iterate over the keys in the object, and those will never be objects. Instead, you should retrieve the item before checking its type.

for(let key in employee){
    let value = employee[key];
    if(typeof value=='string' || typeof vlaue=='number'){
        console.log(key + ":" + value);
    }
    else if(typeof value=='function'){
        console.log(key + ":" + value());
    }
}

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074415

How am i supposed to retrieve the value for those two properties?

The function is the value of those properties. If you want to get the return value of the function, you have to call it.

Note that the typeof check you're doing in your for-in loop is unnecessary. The eachEle variable is the property name, not the property value. In a for-in loop, the name will always be a string. (Not all properties are named with strings, but for-in only covers the ones that are.)

You want to get the value of the property, check if it's a function, and if so call it:

for (let name in employee){
    let value = employee[name];
    if (typeof value === "function") {
        value = employee[name]();
    }
    console.log(name + ":" + value);
}

Live Example:

let employee = {
    emp_firstname: "Prasanta",
    emp_lastname: "Banerjee",
    emp_fullname: function(){
        return (this.emp_firstname + " " + this.emp_lastname);
    },
    emp_id: 673630,
    emp_horizontal:"QEA",
    emp_vertical: "Insurance",
    joining_date: "22/12/2017",
    emp_salary : 13579,
    emp_bonus : function(){
        return (this.emp_salary*1);
    }
};
for (let name in employee){
    let value = employee[name];
    if (typeof value === "function") {
        value = employee[name]();
    }
    console.log(name + ":" + value);
}


You said you just wnated to change the loop, but another approach is to change the object definition to use an accessor property rather than an explicit function:

let employee = {
    emp_firstname: "Prasanta",
    emp_lastname: "Banerjee",
    get emp_fullname() {
//  ^^^             ^^
        return (this.emp_firstname + " " + this.emp_lastname);
    },
    emp_id: 673630,
    emp_horizontal:"QEA",
    emp_vertical: "Insurance",
    joining_date: "22/12/2017",
    emp_salary : 13579,
    get emp_bonus() {
//  ^^^          ^^
        return (this.emp_salary*1);
    }
};

Then the loop doesn't have to check:

for (let name in employee){
    console.log(name + ":" + employee[name]);
}

Live Example:

let employee = {
    emp_firstname: "Prasanta",
    emp_lastname: "Banerjee",
    get emp_fullname() {
//  ^^^             ^^
        return (this.emp_firstname + " " + this.emp_lastname);
    },
    emp_id: 673630,
    emp_horizontal:"QEA",
    emp_vertical: "Insurance",
    joining_date: "22/12/2017",
    emp_salary : 13579,
    get emp_bonus() {
//  ^^^          ^^
        return (this.emp_salary*1);
    }
};
for (let name in employee){
    console.log(name + ":" + employee[name]);
}

That works because when you get the value of an accessor property, its accessor function is run behind the scenes and that function's return value is provided as the property value.

Upvotes: 1

Related Questions