0

I am trying to JSON.stringify(docArray) which contains a nested array in the object that is not being stringified.

console.log(docArray); - returns the information below

(3) [{…}, {…}, {…}]
0:
 docName: "name"
 refDoc: Array(3)
   0: "doc1"
   1: "doc2"
   2: "doc3"
   length: 3
   __proto__: Array(0)
 __proto__: Object

When I stringify the object I receive the following :

console.log(JSON.stringfy(docArray));

[{"docName:"name","refDoc":[]},

Javascript to create the docArray:

docArray = [];
for(let i = 0; i < data.length; i++){
  let docElement = {
     "docName": data[i].getAttribute("Title),
     "refDoc": getDoc(data[i].getAttribute("Title"))
 }
};
docArray.push(docElement);
...
console.log(docArray);
console.log(JSON.stringify(docArray));

getDocs function Javascript

function getDoc(document){
  var refDocs = [];
  $SP().list("listName").get({
    fields:"Title,Parent",
    where:'Title ="'+document+'"',
    }, function(data){
      for(let i = 0; i < data.length; i++){
        refdocs.push(data[i].getAttribute("Parent"));
      }
    });
   return refdocs;
}

I do have a similar project that creates an array in the same fashion though that array does not have a problem being stringified. I have attached pictures below for the console.log outputs of that array.

console.log(array) - working screenshot

console.log(JSON.stringify(array)) - working JSON.stringify

Any help would be greatly appreciated, thank you!

Derek Pollard
  • 6,238
  • 6
  • 34
  • 51
  • Wait how is this question related to `How do I return the response from an asynchronous call?`. He didn't mention anything about `AJAX` calls and he is simply trying to stringify his array. lol – Alex Jun 14 '18 at 15:36
  • Is the code you provided exactly the same as the code you are running? The `refDocs` variable has different capitalizations in your code, so running this would probably give you an error that the lower case `refdocs` has no method `push` because you haven't declared it as an array. – mpallansch Jun 14 '18 at 15:38
  • False duplicate. – Alex Jun 14 '18 at 15:39
  • @Alex that `.get()` function call in the `getDoc()` function looks like an asynchronous API to me. – Pointy Jun 14 '18 at 15:40
  • @Pointy his question is related to something completely different though. Just because you see a `.get()` in the question, doesn't mean that is the context of his question. :) – Alex Jun 14 '18 at 15:41
  • His scope is `JSON.Stringify()` and `Nested Arrays`. – Alex Jun 14 '18 at 15:42
  • @Alex look at the code. The `refdocs` array is built in the callback to that (apparently) asynchronous `.get()` call, and yet the surrounding function expects to be able to return the array. That's why the array is empty when the attempt is made to stringify it. – Pointy Jun 14 '18 at 15:42
  • It'd be nice if the OP could confirm this or not. – Alex Jun 14 '18 at 15:44
  • @Alex, seems like an obvious duplicate to me, for all the reasons _pointed_ out by _Pointy_, – H77 Jun 14 '18 at 15:46
  • @H77 That's okay, I was mostly focused on the context of his question, not on the code. – Alex Jun 14 '18 at 15:56
  • @mpallansch sorry that was a mistype on my part in my code it is 'refdocs' – Zachary Roberts Jun 14 '18 at 16:01
  • @Pointy refdocs array does return the information back to the docArray - the array is returning empty when i am trying to stringify it. Would it still be unable to stringify the array even after it has returned to docArray? Sorry getting new to Javascript and programming – Zachary Roberts Jun 14 '18 at 16:13
  • @ZacharyRoberts put a `console.log()` call *inside* that callback function (the place you `.push()` the elements onto the array) and also one before the `return` statement at the end of `getDoc()`. You should be able to see what's happening then, and there is a lot of information at the linked duplicate. – Pointy Jun 14 '18 at 16:29

0 Answers0