Note that there are some explanatory texts on larger screens.

plurals
  1. POLast Array Dictionary Object is Null - Node JS
    primarykey
    data
    text
    <p>A bit of a strange problem I've run into. I'm looping through an array of message objects which contain various information including date, topic data, and user content. For each index I grab three items and create a dictionary which then gets pushed into a new array. Once the loop has completed the new array has all of the proper information, with the exception of the last index. Of the three dictionary values in the last index, the last item (date object) is null. I've worked around the issue by looping using a forEach as an alternative like so:</p> <p>Original non-working method:</p> <pre><code>var tempArray = []; for(var k = 0; k &lt; msgArray.length; k++) { var newDict = {msg:msgArray[k], topId:msgArray[k].topicId, created:msgArray[k].dateTime}; tempArray.push(newDict); } </code></pre> <p>Alternative working method:</p> <pre><code>var tempArray = []; msgArray.forEach(function(messa){ var newDict = {msg:messa, topId:messa.topicId, created:messa.dateTime}; tempArray.push(newDict); }); </code></pre> <p>And a capture from the debugger: <img src="https://i.stack.imgur.com/Dttje.png" alt="bad temp array"></p> <p>Alternative method capture from debugger: <img src="https://i.stack.imgur.com/zsSDQ.png" alt="good temp array"></p> <p>The workaround works fine, but it would be nice if someone could explain why this is occurring, just for my own learning/curiosity. I appreciate any comments. Thanks. </p> <p>Edit: Although it would seem like there is something fishy with msgArray, it's a run of the mill array with valid content in each index...structured like so:</p> <p>msgArray[0] - message (Object) - topicId (String) - dateTime (Date)</p> <p>...and so on. I would think if there were an issue with the original array it would effect both methods of parsing out into dictionaries, however it does not. Part of the reason this is such an odd occurrence. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload