M
M
Magnus Keef2018-12-14 01:27:50
JavaScript
Magnus Keef, 2018-12-14 01:27:50

How to form an array of objects?

5c12dc0aac366153397202.png
Here is my code and it doesn't work quite right.

var mass = [];
function queryDatabasePunish() {
    request = new Request(`SELECT punishments.num_punishment, breakdowns.coast, punishments.name_breakdown FROM breakdowns INNER JOIN punishments ON breakdowns.num_breakdown = punishments.num_breakdown WHERE (punishments.num_ptc = '${password}')`, function (err, rowCount, rows) {
        console.log(rowCount + ' row(s) вернулось из штрафов');
        PunCount = rowCount;
    });
    request.on('row', function (columns) {
        columns.forEach(function (column) {
            punishments[column.metadata.colName] = column.value;//заполнение объекта
        });
        mass.push(punishments);
        console.log(mass);
    });
    abcdef.execSql(request);
}

It overwrites the objects in the array (the result in the picture) turned out to be an array with three identical objects 5c12dcba09076905808574.png
And there should be such entries:5c12dc0aac366153397202.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergmit, 2018-12-14
@SecurityYourFingers

I think it should help:

var punishments = {};
columns.forEach(function (column) {
       punishments[column.metadata.colName] = column.value;//заполнение объекта
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question