3 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
這樣的事情應(yīng)該可以解決問題。它的客戶端轉(zhuǎn)型雖然:
let carArr = []
const query = "SELECT brand, model, color from cars"
mysql.query(query, values, (err, result) => {
for (let i = 0; i < result.length; i++) {
// Get the index of the brand in the carArr array
let brandIndex = carArr.map(c => c.brand).indexOf(result.brand)
// If the brand is not already in carArr, enter a new value for it
if(brandIndex == -1)
carArr.push({"brand": result.brand, "cars": [{"model": result.model, "color": result.color}]})
// If the brand already exists, add the car type to this specific barnd
else
carArr[brandIndex].cars.push({"model": result.model, "color": result.color})
}
})
添加回答
舉報(bào)