Syntax error How to get only the first BOT ID from thes JavaScript array?

How to get only the first BOT ID from thes JavaScript array?



Let’s say we have records with BOTID and Name of assigned users −

let objectArray = [
   { BOTID: "56", Name: "John" },
   { BOTID: "57", Name: "David" },
   { BOTID: "58", Name: "Sam"},
   { BOTID: "59", Name: "Mike" },
   { BOTID: "60", Name: "Bob" }
];

We know the array starts from index 0. If you want to access the first element from the above array, use the below syntax −

var anyVariableName=yourArrayObjectName[index].yourFieldName;

Example

let objectArray = [
   { BOTID: "56", Name: "John" },
   { BOTID: "57", Name: "David" },
   { BOTID: "58", Name: "Sam"},
   { BOTID: "59", Name: "Mike" },
   { BOTID: "60", Name: "Bob" }
];
const output = objectArray[0].BOTID;
console.log(output);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo49.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo49.js
56
Updated on: 2020-09-03T06:21:00+05:30

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements