在nodejs中循环问题

我有一个nodejs的问题。 我试着用node-xlsx库读取一个xml文件。

libreary正确地读取文件,并在数组中添加信息。 问题是当我尝试与这个数组挂载其他数组。

码:

module.exports = { getConnections: function () { var obj = xlsx.parse('xxxxx.xls'); // parses a file var connection = { customer: "", Type: "", ip: "", user: "", pass: "" } var connections = []; //loop through all rows in the sheet for (var j = 0; j < obj[0]['data'].length; j ++) { //add the row to the rows array //rows.push(sheet['data'][j]); if (obj[0]['data'][j].length > 4) { var a = connection; connection.customer = obj[0]['data'][j][0]; connection.Type = obj[0]['data'][j][2] connection.ip = obj[0]['data'][j][3] connection.user = j; connections.push(connection) //console.log(connection) } } console.log(connections) return connections; } } 

如果我看到,console.log(连接),任何对象被correcly创build。 我将这个对象添加到数组中以返回所有信息。

但是,如果我阅读第二个控制台日志,console.log(连接),看看我返回的数组中的所有对象是相同的。 所有像循环的最后一个值..