gpt4 book ai didi

Javascript:如何从 mysql 查询中过滤对象数组推送

转载 作者:行者123 更新时间:2023-11-29 20:49:25 24 4
gpt4 key购买 nike

我从 mysql 得到了一个数组

var notes = []
db.query('SELECT * FROM test WHERE working = 0')
.on('result', function(data){
// Push results onto the notes array
notes.push(data)
})
.on('end', function(){
// Only emit notes after query has been completed
console.log(notes)
})

note = [ RowDataPacket { id: 12, link: 'This is a random 74 note', working: 0 },
RowDataPacket { id: 16, link: 'This is a random 80 note', working: 0 },
RowDataPacket { id: 44, link: 'This is a random 29 note', working: 0 } ]

我尝试过滤一些对象,但它不起作用。

notes = notes.filter(function(obj) {
return obj.id !== data.noteid;
});

它返回相同的数组

note = [ RowDataPacket { id: 12, link: 'This is a random 74 note', working: 0 },
RowDataPacket { id: 16, link: 'This is a random 80 note', working: 0 },
RowDataPacket { id: 44, link: 'This is a random 29 note', working: 0 } ]

有什么想法吗?

最佳答案

我发现这个函数可以从数组中删除一个对象。

var removeByAttr = function(arr, attr, value){
var i = arr.length;
while(i--){
if( arr[i]
&& arr[i].hasOwnProperty(attr)
&& (arguments.length > 2 && arr[i][attr] === value ) ){
arr.splice(i,1);
}
}
return arr;
}

removeByAttr(notes, 'id', parseInt(data.noteid))

一切正常:)

关于Javascript:如何从 mysql 查询中过滤对象数组推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38179193/

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