从nodejs传递关联数组到客户端结束为空
从nodejs服务器端我已经写了下面的代码
socketIOobj.to(clientID).emit('send-prev-conversation-data',{ prevConversation: JSON.stringify(finalOutputArray) });
在这里,如果我做console.log(finalOutputArray),我得到了下面的输出
[ [ convId: 11, no: 1, time: 2016-12-27T17:36:19.000Z, subjectline: 'message005' ], [ convId: 10, no: 2, time: 2016-12-26T18:02:17.000Z, subjectline: 'fdf' ], [ convId: 4, no: 2, time: 2016-12-25T09:46:12.000Z, subjectline: 'cds' ], [ convId: 3, no: 4, time: 2016-12-25T09:33:39.000Z, subjectline: 'gg2' ] ]
但是,当我尝试使用下面的代码在客户端接收finalOutputArray数组值
socket.on( 'send-prev-conversation-data', function( data ) { console.log(data.prevConversation); var aa=JSON.parse(data.prevConversation); console.log(aa); console.log(aa[0]); socket.removeAllListeners('send-prev-conversation-data'); });
我得到的输出为:
[[],[],[],[]] Array [ Array[0], Array[0], Array[0], Array[0] ] Array [ ]
在这里,我的问题是,我如何得到确切的数组,我已经创build了像nodejs :
[ [ convId: 11, no: 1, time: 2016-12-27T17:36:19.000Z, subjectline: 'message005' ], [ convId: 10, no: 2, time: 2016-12-26T18:02:17.000Z, subjectline: 'fdf' ], [ convId: 4, no: 2, time: 2016-12-25T09:46:12.000Z, subjectline: 'cds' ], [ convId: 3, no: 4, time: 2016-12-25T09:33:39.000Z, subjectline: 'gg2' ] ]
在客户端,这样我就可以使用它来显示客户端浏览器中的数据。
似乎发生了什么事情是你正在向数组中添加属性而不是在这个问题或这个问题中描述的对象,这几乎就是你的问题。
我可以从console.log(finalOutputArray)
的输出console.log(finalOutputArray)
,它里面的每个元素都是一个数组而不是一个对象。 可能你已经定义了你的数组元素,像这样:
var item = []; item.convId = ... item.no = ... ...
尽pipe可以在Javascript中向数组添加非数字键,但正如您所看到的那样,JSON.stringify不支持此操作,只能将arrray的数字键串化。 修复很简单。 只需使用一个对象:
var item = {}; item.convId = ... item.no = ... ...
感谢给我提示解决问题。 现在我find如下的解决scheme:
以前我声明multidimensional array为:
var finalOutputArray031 = []; var tempFlag031 = -1; // inside some for loop tempFlag031++; finalOutputArray031[tempFlag031] = []; // it creates array of array finalOutputArray031[tempFlag031]['convId'] = tempKey031; finalOutputArray031[tempFlag031]['no'] = 1; finalOutputArray031[tempFlag031]['time'] = ii.conversation_time; After that when I try to JSON.stringify(finalOutputArray031)
然后我得到的输出
[[],[],[],[]]因为我的多国数组成为Array [Array [0],Array [0],Array [0],Array [0]]
If I want to get my multidimenational array value exactly as I created in node.js then, I have to define like var finalOutputArray031 = []; var tempFlag031 = -1; // inside some for loop tempFlag031++; finalOutputArray031[tempFlag031] = {}; // instead of array it creates object finalOutputArray031[tempFlag031]['convId'] = tempKey031; finalOutputArray031[tempFlag031]['no'] = 1; finalOutputArray031[tempFlag031]['time'] = ii.conversation_time; Then, in client side when I do as below: var prevConvArray = JSON.parse(data.prevConversation); console.log(prevConvArray); for(var i=0;i<prevConvArray.length;i++){ console.log(prevConvArray[i]); } Then I get what exactly I want Array [ Object, Object, Object, Object ] Object { convId: 11, no: 1, time: "2016-12-27T17:36:19.000Z", subjectline: "goutam dolai goutam dolai" } Object { convId: 10, no: 2, time: "2016-12-26T18:02:17.000Z", subjectline: "fdf" } Object { convId: 4, no: 2, time: "2016-12-25T09:46:12.000Z", subjectline: "cds" } Object { convId: 3, no: 4, time: "2016-12-25T09:33:39.000Z", subjectline: "gg2" }