nodejs mysql查询数据以循环数组
Posted
技术标签:
【中文标题】nodejs mysql查询数据以循环数组【英文标题】:nodejs mysql query data to loop array 【发布时间】:2021-06-08 07:35:17 【问题描述】:我正在使用nodejs
和mysql
从数据库中检索数据。
我需要得到如下结果:
var dictionary, set_lang;
dictionary =
"english":
"_availablestorage": "Available Storage",
"_prayschedule": "Pray Schedule",
"_mainmenu": "Main Menu",
"_temperature": "Temperature",
"_calibration": "Calibration",
"_mosque": "Mosque"
,
"indonesia":
"_availablestorage": "Penyimpanan Tersedia",
"_prayschedule": "Jadwal Sholat",
"_mainmenu": "Menu Utama",
"_temperature": "Suhu",
"_calibration": "Kalibrasi",
"_mosque": "Masjid"
;
这是我在 js 文件上的代码:
con.query('SELECT * FROM tbl_language;', (err, results)=>
//Here I do not know how to do.
//res.json(results);
);
这里是sql fiddle。
【问题讨论】:
【参考方案1】:const dictionaryData = [
id: 0, language: 'English' ,
id: 1, language: 'Indonesian'
]
const languageData = [
language_id: 0,
key_language: '_availablestorage',
description: 'Available Storage'
,
language_id: 0,
key_language: '_prayschedule',
description: 'Pray Schedule'
,
language_id: 0,
key_language: '_mainmenu',
description: 'Main Menu'
,
language_id: 1,
key_language: '_availablestorage',
description: 'Tersedia'
,
language_id: 1,
key_language: '_prayschedule',
description: 'Jadwal'
,
language_id: 1,
key_language: '_mainmenu',
description: 'Menu Utama'
]
查询的响应是这样的,那么下面的代码就可以了。
const dictionary =
dictionaryData.forEach((data) =>
const setLang =
languageData.forEach((language) =>
if (data.id === language.language_id)
setLang[language.key_language] = language.description
)
dictionary[data.language] = setLang
)
console.log(dictionary)
回复是:
English:
_availablestorage: "Available Storage",
_mainmenu: "Main Menu",
_prayschedule: "Pray Schedule"
,
Indonesian:
_availablestorage: "Tersedia",
_mainmenu: "Menu Utama",
_prayschedule: "Jadwal"
现在用您的查询循环更改 foreach 循环。它会起作用。 附上fiddle link
【讨论】:
以上是关于nodejs mysql查询数据以循环数组的主要内容,如果未能解决你的问题,请参考以下文章