gpt4 book ai didi

javascript - 如何访问此对象的属性?

转载 作者:行者123 更新时间:2023-11-30 08:03:24 24 4
gpt4 key购买 nike

我在 node.js 和 socket.io 工作,基本上我监听一个套接字 disconnect 事件并调用一个方法。 app.update()

socket.on('disconnect', function (data) {
app.update();
});

// This is in another file, and the id is static, but I am more concered with the
// messages object that is passed through.
update: function() {
models.Message.find({_id: "532c8d9ce889ed4c21538630"}, function(err, messages) {
console.log(messages)
});
}

这就是messages对象的样子

[ { username: 'Marcus',                                         
connected: true,
_id: 532c8d9ce889ed4c21538630,
__v: 0 } ]

我不确定如何访问带有方括号的对象,但基本上我想更新该对象,以便我可以将 connected 设置为 false。

我尝试了 messages['connected']

最佳答案

您有一个对象数组。因此,您需要访问数组的第一个元素,然后是“已连接”属性。这样做:

messages[0].connected = false;

关于javascript - 如何访问此对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22567801/

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