我有函数 showEmployee(i)
,它返回了我的数组对象的索引。
使用索引值我需要显示该返回索引的值。
我无法弄清楚如何做。我疲倦的 congole.log(i)
返回 undefined
以下是我的代码。请帮助
Employees =[
{'name':'Arun','role':'Developer'},
....
];
showEmployee(i){
console.log(i); // this returns the index value of the array
}
这是在JavaScript中执行此操作的方法,我们可以通过执行<<arrayName>>[<<index of the array element>>]
访问数组元素,请参阅下面的snippet,让我知道是否解决了您的问题!
如果要返回数组中的特定值,则可以在showEmployee()
函数中执行return Employees[i].name
或return Employees[i].role
。
注意:
-
变量(例如(
Employees
)需要使用语法var << variable name >> = <<something>>
。
)定义 应像
一样定义函数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;
}