我想在MySQL中进行批量插入。我也遵循this问题进行理解。用户发布了一个答案,该答案需要数据数组,然后插入数据库。
我有这样存储的数据
'116':
{
lat: '31.53867',
long: '74.41279666666667',
timestamp: '2018-07-28 15:56:15'
},
'117':
{
lat: '31.53863',
long: '74.41278',
timestamp: '2018-07-28 15:56:20'
},
'118':
{
lat: '31.538645',
long: '74.41285',
timestamp: '2018-07-28 15:58:39'
}
}
由于我是node的新手,所以我不知道如何将整个字典放入数据库。
或者,如果不可能的话,如何将数据转换为这种格式,以便可以将其插入查询中
var values = [
['31.53863', '74.41285', '2018-07-28 15:58:39'],
['31.53863', '74.41285', '2018-07-28 15:58:39'],
['31.53863', '74.41285', '2018-07-28 15:58:39']
];
任何帮助将不胜感激。
答案 0 :(得分:0)
您可以使用for in循环创建这样的数组:
var values = [
['31.53863', '74.41285', '2018-07-28 15:58:39'],
['31.53863', '74.41285', '2018-07-28 15:58:39'],
['31.53863', '74.41285', '2018-07-28 15:58:39']
];
尝试一下
var oldData = [{
lat: '31.53867',
long: '74.41279666666667',
timestamp: '2018-07-28 15:56:15'
},
{
lat: '31.53863',
long: '74.41278',
timestamp: '2018-07-28 15:56:20'
},
{
lat: '31.538645',
long: '74.41285',
timestamp: '2018-07-28 15:58:39'
}
];
var newArr = [];
oldData.map(function(obj, index) {
var val = [];
for (index in obj) {
val.push(obj[index]);
}
newArr.push(val)
});
console.log(newArr);
// Expected Output:
/*
[
['31.53863', '74.41285', '2018-07-28 15:58:39'],
['31.53863', '74.41285', '2018-07-28 15:58:39'],
['31.53863', '74.41285', '2018-07-28 15:58:39']
];
*/