Display Array item using its Index value

aw3123

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
}
Naren Murali

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java - How to get item index from an array with its value?

React - Remove item from array using its name / value

Updating an item within an array and maintain its index

display array index value JavaScript

Access and display an image using its index

Finding an item in an array and updating its value

Using a value in array as index

Get max value and its index in numpy array

Replace value in an array by its index in a list

Variable in array's index not changing the its value

Delete Item from Array in Angular at Index value

Javascript - Find Value of Item in Array by Knowing Index

PHP ARRAY get index of item for certain value

Accessing the value of a zip object using its index

return closest item to a given value in a list and its index

How to get the ValueItem value of ListBox item by its index

How do you remove an item from a dictionary by its index value?

can't get my array to output its value, using the index number

Getting the array index for an item based on one of its properties

Clickhouse - Output each array item and its index/position

display each item in the array without using a loop

Display highest value and index number in array

How to display the value of an array by the index number?

Display first index number of array for particular value

return array item by using index in mongodb with php

How to display object of array by its index and property with rezct hooks

This is a program to search a number and also display its index position if found in an array

how to search for the value with a certain index in an array and modify its value?

What is the type of the index of an array using a range as its index?

TOP Ranking

HotTag

Archive