简体   繁体   中英

Passing associative arrays from nodejs to client side ends up empty

From nodejs server side I have written the below code

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

Here, if I do console.log(finalOutputArray), I got the below output

[ [ 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

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:

[ [ 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. 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. 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] ]

          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" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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