我有一个像这样的对象数组:
var msg = [
{ msg: 'text' },
{ src: 'pic.jpg',id: 21,title: 'ABC' }
];
我尝试通过手动迭代服务器端的对象来清理值,但module只会在控制台中返回[ '[object Object]', '[object Object]' ]
。
socket.on('message', function(msg){
if (io.sockets.adapter.rooms[socket.id][socket.id] == true)
{
var fun = [];
for(var j in msg){
fun[j] = sanitizer.sanitize(msg[j]);
};
console.log(fun);
io.sockets.in(socket.room).emit('message',fun);
}
});
有谁能告诉我如何正确消毒对象?
答案 0 :(得分:1)
这样的事情可以解决问题:
var fun = [];
for(var i = 0; i < msg.length; i++){ // Don't use for...in to iterate over an array.
fun[i] = msg[i]; // Copy the current object.
for(var j in fun[i]){ // Iterate over the properties in this object.
fun[i][j] = sanitizer.sanitize(fun[i][j]); // Sanitize the properties.
};
};