0

I've function showEmployee(i) which returns me the index of an array object.

Using the index value i need to show the value of that returned index.

I could not figure out how to do it. I tired congole.log(i) which returns undefined

Below is my code. Please help

Employees =[
    {'name':'Arun','role':'Developer'},
    ....
  ];

showEmployee(i){
   console.log(i); // this returns the index value of the array
}
3
  • your i is the current object in the array ? Commented Nov 26, 2017 at 8:17
  • 'i' is index? currect list? current object? Commented Nov 26, 2017 at 8:19
  • 2
    it's incomplete code. and incomplete and unclear question. need to be edited. Commented Nov 26, 2017 at 8:22

1 Answer 1

0

Here is the method to do this in Javascript, we can access array element by doing <<arrayName>>[<<index of the array element>>], please refer the below snippet and let me know if this fixes your issue!

if you want to return a specific value in the array, you can do return Employees[i].name or return Employees[i].role in the showEmployee() function.

Note:

  1. variables like (Employees) need to defined using the syntax var << variable name >> = <<something>>.

  2. functions should be defined like

    function <<function name>>(<< function parameters){
    
     }
    

var Employees = [
    {'name':'Arun','role':'Developer'},
    {'name':'Arun2','role':'Developer2'},
    {'name':'Arun3','role':'Developer3'},
    {'name':'Arun4','role':'Developer4'},
    {'name':'Arun5','role':'Developer5'}
  ];

function showEmployee(i){
   console.log(Employees[i]); // this returns the index value of the array
}

showEmployee(2);
showEmployee(3);
showEmployee(4);
.as-console {
    height: 100%;
}
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.