B
B
Bur Ov2016-02-04 15:32:10
MySQL
Bur Ov, 2016-02-04 15:32:10

How to convert an array received from mysql to another form?

Code (node.js):

var mysql      = require('mysql');

var connection = mysql.createConnection({
  host     : '*********',
  user     : '*********',
  password : '**********',
  database : '**********'
});

module.exports = (arg, callback) => {
  
connection.connect();

connection.query('SELECT user FROM users', function(err, rows, fields) {
  if (err) throw err;

  return callback('Текст: ' + JSON.stringify(rows));
});

connection.end();
        
}

I get: Text: [{"user":317959591},{"user":222222},{"user":3333333}]
How to make the array look like: ['317959591', '222222', '3333333']

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
ZLOFENIX, 2016-02-04
@burov0798

Something like this.
if (err) throw err;
var array = new Array();
for (var t in rows)
array.push(t['user']);
return callback('Text: ' + JSON.stringify(array));

P
PerfectLab, 2016-02-04
@PerfectLab

var mysqlText = '[{"user":317959591},{"user":222222},{"user":3333333}]';
var mysqlArray = JSON.parse(mysqlText);
var array = mysqlArray.map(function(i){ return i.user });
console log(array);
// [317959591, 222222, 3333333]

K
keslo, 2016-02-04
@keslo

...
connection.query('SELECT user FROM users', function(err, rows, fields) {
  if (err) throw err;
  return callback('Текст: ' + rows.map(function(row) { return row.user }));
});
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question