1

I'm reading back record sets in an express server using the node mssql package. Reading back the values outputs an array of Json objects as expected.

Now I need to modify the Email propoerty value of each Json object. So I tried looping through the recordset and changing the value at each index:

            var request = new sql.Request(sql.globalConnection);
            request.input('p_email', sql.VarChar(50), userEmail);
            request.execute('GetDDMUserProfile', function(err, recordsets, returnValue) {      

               for (var i = 0; i < recordsets.length; i++){
                    recordsets[i].Email = "[email protected]";

                }

                console.log(recordsets);

            });

But instead of modifying the Emailvalue of each Json object, this code just appends a new email property to the last Json object.

How can you map new property values to an array of JSON objects?

Example output: An example of the output is shown below, where a new Email property has been added to the end of the array instead of changing each existing property value:

[  
   [  
      {  
         ID:[  
            4
         ],
         UserName:"Brian",
         Email:"[email protected]"
      },
      {  
         ID:[  
            5
         ],
         UserName:"Brian",
         Email:"[email protected]"
      }      
   Email:'[email protected]' ]
]

1 Answer 1

1

The issue here is that your dataset appears to not be an array of JSON objects but, rather, an array of arrays of JSON objects. If you know for certain that you'll always have only one array in the top-most array, then you can solve the problem like this:

recordsets[0][i].Email = "[email protected]";

to always target the first array in the top-most array. However, if the top-most array could potentially have more than one array, that'll be a different kind of issue to solve.

Sign up to request clarification or add additional context in comments.

5 Comments

So I tried this previously recordsets[0][i].Email = "[email protected]"; and it only modifies the Email value of the first json object. All subsequent object's Email values stays the same. For example hastebin.com/tanifucitu.pl Any ideas why that is?
It's because you're doing recordsets.length in your for loop. Since recordsets only has one element – the array of JSON objects – the for is only looping once. You'll need to change it to recordsets[0].length to get the actual count of the JSON objects in the interior array.
@BrianJ as in the original answer above, you're using an array of arrays. So first array only has 1 entry. In your for loop if you don't change i < recordsets.length to i < recordsets[0].length your loop only runs once. Hope that's clear?
@Beebee jinx. :-)
yep clear as day, I needed to change the index in the loop because of the nested arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.