user752746
user752746

Reputation: 617

How to get value from array of object

I'm learning Node.js and using mssql to retrieve employee data. I get the following recordset back and having trouble grab the first and last name:

{ recordsets: [ [ [Object] ] ],
  recordset: [ { uid: 'jd123', fName: 'John', lName: 'Doe' } ],
  output: {},
  rowsAffected: [ 1 ] 
}

I have tried the following but had no luck:

D:\LearnNodeJs\Udemy\routes\processes.js

// var submitter = recordset[0]['fName']; // TypeError: Cannot read property 'fName' of undefined
                               ^
//var submitter = recordset[0].fName; // TypeError: Cannot read property 'fName' of undefined
var submitter = recordset.fName; // undefined

console.log(submitter);

Upvotes: 0

Views: 1492

Answers (2)

HolyMoly
HolyMoly

Reputation: 2080

what is the name of your object? try:

const firstNames = someObject.recordset.map(record => record.fName) to get the first name(s). modify on that as needed to get what you want.

Upvotes: 0

Bergur
Bergur

Reputation: 4057

Seems you are doing it right

https://jsfiddle.net/9co7wf5t/

const response = {
    recordsets: [ [ [Object] ] ],
  recordset: [ { uid: 'jd123', fName: 'John', lName: 'Doe' } ],
  output: {},
  rowsAffected: [ 1 ] 
};

console.log(response.recordset[0].fName);

Upvotes: 1

Related Questions