Otherwise, if you are actually wanting to display the whole array on the webpage, you can use something like the following:
<pre id="result">
</pre>
function displayAll(data) {
let array = JSON.parse(data);
let result = document.querySelector("#result");
result.innerHTML = JSON.stringify(array, null, " ");
}
Makes sense. Thanks for the explanation. But what if I want the value of items outside the function without invoking the displayAll function?
function displayAll(data){
items = JSON.parse(data);
console.log("Printing items inside");
console.log(items);
}
The reason I am trying to get the value of items outside the displayAll function is that this function is the first point where I’m getting data back from a Direct Web Remoting call which is similar to Ajax call. Here’s what my function looks like with DWR call before it.
So when I call this displayList method in my ManageDisplay java class like this ManageDisplay.displayList(displayAll); , I am capturing the data for the first time inside displayAll(data) function.
One option I’ve tried is this and it works but has following issue:
let items = new Array();
function displayAll(data){
var parsedData = JSON.parse(data);
items.push(parsedData);
}
It will print fine except that, there is another array inside an array which I don’t want. It adds an additional [] inside the items array coming from inside of displayAll function. This is how it prints outside with above changes:
Then you get the answer you got. Until you invoke the displayAll function, items has no value.
Consider a simpler function.
function addtwo() {
num += 2;
}
All it does it add 2 to num when addtwo() is called.
Q: What’s the value of num if you dont invoke the function?
A: Whatever you started with. You havent done anything, so nothing has changed. Inertia rules the day!
I need an answer to one thing before I respond to this, to confirm my suspicions:
When you ran the code I gave you, which “Printing items” line showed up first in the log?
I’m… going to try not to go too into the weeds on this, but that’s what we call a race condition.
it saying to the code “Go and invoke this command, which (i’m guessing) goes and fetches some data, and then pass it to the displayAll function”.
Javascript, in its efforts not to freeze up the browser while waiting for a fetch request from another server, calls this function asynchronously. Meaning it dispatches a separate process to go do that, while it continues reading your code.
So now we’ve got two bits of code running at the same time:
The main bit of the code, which skips over the function declaration for displayAll (because it doesnt need it) and moves on to the console.log line after it.
The displayList bit of code, which is fetching data.
The trick is that both these pieces of code are going to be interacting with the items variable - the main bit is trying to read it, and the displayList bit is trying to write to it.
One of them gets there first; which one is not guaranteed.
In your case, the main thread got there first, printed out items, which at that point had not been edited and so was empty.
Then the displayList thread got there, and filled it.
So how do you fix that.
Well, basically, you make the displayAll function do anything and everything you want to do with items; or call other functions that use it, after you know you’ve written the array.