gpt4 book ai didi

javascript - 如何删除 JavaScript RowDataPacket 数组中的重复数据

转载 作者:行者123 更新时间:2023-12-02 23:07:31 25 4
gpt4 key购买 nike

我使用 Nodejs 从 MySQL 服务器获取了数据,我想删除该数组中的重复数据,但是该数组的结构看起来很奇怪,我尝试了各种方法来解决这个问题,但是没有用。

代码

router.get(`/find-users/:queryString`, function(req, res, next) {
let queryString = req.params.queryString;
db.query(
`SELECT userId from project WHERE keyword like "%${queryString}%"`,
function(error, data) {
// console.log(Array.from(new Set(data)));
console.log(data);
}
);
res.json(data);
});

结果数据

[ RowDataPacket { userId: 'arpitjindal97' },
RowDataPacket { userId: 'cs01' },
RowDataPacket { userId: 'samuelcolvin' },
RowDataPacket { userId: 'Jyrno42' },
RowDataPacket { userId: 'LemonyDesign' },
RowDataPacket { userId: 'thruthesky' },
RowDataPacket { userId: 'thruthesky' },
RowDataPacket { userId: 'thruthesky' },
RowDataPacket { userId: 'thruthesky' },
RowDataPacket { userId: 'apollographql' },
RowDataPacket { userId: 'samuelcolvin' },
RowDataPacket { userId: 'Jyrno42' },
RowDataPacket { userId: 'Jyrno42' },
RowDataPacket { userId: 'thruthesky' }
}

我尝试了 console.log(Array.from(new Set(data))); 但结果是相同的。我该如何解决这个问题?

最佳答案

尝试distinct删除重复数据

router.get(`/find-users/:queryString`, function(req, res, next) {
let queryString = req.params.queryString;
db.query(
`SELECT distinct userId from project WHERE keyword like "%${queryString}%"`,
function(error, data) {
// console.log(Array.from(new Set(data)));
console.log(data);
}
);
res.json(data);
});

关于javascript - 如何删除 JavaScript RowDataPacket 数组中的重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57518247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com