如何清理节点中的对象数组?迭代通过它手动返回'object Object'

问题描述 投票:3回答:1

我有一个这样的对象数组:

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);
 }
});

谁能告诉我如何正确消毒对象?

javascript html node.js xss
1个回答
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.
    }; 
};
© www.soinside.com 2019 - 2024. All rights reserved.