1

I have an object and need to get an array for each keys and values. I used Object.keys (also tried Object.getOwnPropertyNames()) and Object.values() but both returned zero length array. I confirmed that the object does has values in it.

Below codes worked fine in jFiddle, but returned zero length array for Object.keys and Object.values calls on my environment. Working jFiddle here: https://jsfiddle.net/ca2xL0bf/

My environment: Eclipse Neon, Tomcat 8.5. Browser: Tested on both Chrome and IE11.

What could be wrong with the code or environment specific?

Thanks,

Alex

function countPLines(data){
  var pl = data.split(',');

  pl.forEach(function(entry){   
      pLines[entry] = (pLines[entry] || 0) + 1;
  });

}

function createChartData(){
  console.log(pLines); 
  console.log(Object.keys(pLines));
  console.log(Object.values(pLines));
}

var pLines = {};
var data = ['a', 'b', 'c,a,b', 'a,b'];

data.forEach(function(ele) {
    countPLines(ele);
});

createChartData();

Screen shot of console

user1941319
  • 375
  • 3
  • 19
  • Are you getting the actual data as a result in a asynchronous function? After fixing the typo (`PLines` !== `pLines`) [the code seems to work as it is](https://jsfiddle.net/wud6f4n3/). – Teemu Oct 15 '19 at 17:43
  • @Teemu-callmewhateveryouwant yes, the typo was only in here not in my actual code. – user1941319 Oct 15 '19 at 17:47
  • Yeah, I guessed that, otherwise you woudn't have got the logs. Do you mean you're getting those empty arrays in the linked fiddle too?. – Teemu Oct 15 '19 at 17:49
  • @Teemu-callmewhateveryouwant Code in JSFddle worked fine. Its just in my environment. Note the log call just before the just before the keys() and values(), it worked fine. – user1941319 Oct 15 '19 at 17:54
  • You've to iterate the data inside the AJAX callback function (or in a separate function which is called from the AJAX callback function), the fetched data is not available at the time you're trying to retrieve it. Please check [this post](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/14220323#14220323). – Teemu Oct 15 '19 at 17:56
  • The data was available because I print out the object just before I call the keys and values function. – user1941319 Oct 15 '19 at 18:01
  • What does the small `i`-icon says when you hover over it? – Teemu Oct 15 '19 at 18:03
  • It said "value below was evaluated just now" – user1941319 Oct 15 '19 at 18:09
  • The console itself is asynchronous, have you tried to iterate the object despite of the seemingly empty keylist? – Teemu Oct 15 '19 at 18:12
  • I changed the createChartData() function to return Object.keys() and log it. It was also an empty array. – user1941319 Oct 15 '19 at 18:19
  • Please take a look at [this](https://stackoverflow.com/a/23392650/1169519) (follow also the "_console lazy_" link Bergi provides). If those post won't help, use the actual debugger, set a breakpoint and retrieve the value when the execution stops at the breakpoint. Then give a change to my first advice ... – Teemu Oct 15 '19 at 18:26
  • 1
    @Teemu-callmewhateveryouwant Thanks for the info and helps. You were right on your asyn console comment. Will need to rearrange the execution flow. – user1941319 Oct 15 '19 at 18:49
  • @Teemu-callmewhateveryouwant Yup, it works now. I put the code inside the init completed event of Datatables (need to plot a graph from data of a Datatables object). Thanks for the time and helps! Learnt something today. !! – user1941319 Oct 15 '19 at 20:53

1 Answers1

0

You need to use same names: pLines vs PLines.

function countPLines(data) {
    var pl = data.split(',');

    pl.forEach(function(entry) {
        pLines[entry] = (pLines[entry] || 0) + 1;
    });
}

function createChartData() {
    console.log(pLines);
    console.log(Object.keys(pLines));
    console.log(Object.values(pLines));
}

var pLines = {};
var data = ['a', 'b', 'c,a,b', 'a,b'];

data.forEach(countPLines);

createChartData();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392