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
}
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:
variables like (Employees
) need to defined using the syntax var << variable name >> = <<something>>
.
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;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments