3

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.

1
  • the problem is emp_fullname is a function type, needs to called like emp_fullname() ´with () Commented Jan 26, 2020 at 13:10

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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]());
  }
}

Comments

1

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]());
    }
}

Comments

1

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]());
        }
    }

Comments

0

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());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.