简体   繁体   English

将关联数组从Node.js传递到客户端最终是空的

[英]Passing associative arrays from nodejs to client side ends up empty

From nodejs server side I have written the below code nodejs服务器端,我编写了以下代码

socketIOobj.to(clientID).emit('send-prev-conversation-data',{ prevConversation: JSON.stringify(finalOutputArray) });

Here, if I do console.log(finalOutputArray), I got the below output 在这里,如果我执行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' ] ]

But, when I try to receive the finalOutputArray array value in client side using below code 但是,当我尝试使用以下代码在客户端接收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');
  });

I got the output as: 我得到的输出为:

[[],[],[],[]]  
Array [ Array[0], Array[0], Array[0], Array[0] ]  
Array [  ]

Here, my question is that, how I get exact array what I have created in nodejs like: 在这里,我的问题是, 如何获得在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' ] ]

in client side, so that I can use it to show the data in client browser. 在客户端,以便我可以使用它在客户端浏览器中显示数据。

Seems like what's happening is that you are adding properties to an array instead of object which is described in this question or this question which is almost exactly your problem. 好像发生了什么是您要添加的属性添加到数组的对象,而不是它在描述这个问题,或者这个问题,这几乎是完全你的问题。

I can tell this from the output of console.log(finalOutputArray) , where each element inside of it is an array instead of an object. 我可以从console.log(finalOutputArray)的输出中看出这一点,其中的每个元素都是数组而不是对象。 Probably you have defined your array elements like this: 可能您已经定义了数组元素,如下所示:

var item = [];
item.convId = ...
item.no = ...
...

Although you can add non-numerical keys to arrays in Javascript, but as you can see JSON.stringify doesn't support this and only stringifies numerical keys of the arrray. 虽然您可以在Javascript中将非数字键添加到数组中,但是如您所见,JSON.stringify不支持此功能,而仅对错误的数字键进行字符串化。 The fix is simple. 解决方法很简单。 Just use an object instead: 只需使用一个对象代替:

var item = {};
item.convId = ...
item.no = ...
...

Thanks for giving me the hints to solve the problem. 感谢您提供给我解决问题的提示。 Now I find the solution as below: 现在,我找到以下解决方案:

Previously I declare the multidimensional array as: 以前,我将多维数组声明为:

      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)

then I get the output like 然后我得到的输出像

[[],[],[],[]] Because, my multidimenational array becomes, Array [ Array[0], Array[0], Array[0], Array[0] ] [[],[],[],[]]因为,我的多维度数组变成了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" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM