节点 - 从对象数组中获取值

时间:2018-01-17 00:48:47

标签: javascript node.js

我正在学习Node.js并使用mssql来检索员工数据。我得到了以下记录集,并且无法获取名字和姓氏。

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

我尝试过以下但没有运气。

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

非常感谢任何建议,谢谢您的帮助!

2 个答案:

答案 0 :(得分:1)

好像你做对了

https://jsfiddle.net/9co7wf5t/

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

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

答案 1 :(得分:0)

你的对象叫什么名字?尝试:

const firstNames = someObject.recordset.map(record => record.fName)获取名字。根据需要修改,以获得你想要的。