How to Build Object Array

Asked

Viewed 1,021 times

0

I’m trying to build an Object Array in Javascript, but the result is not as expected. That’s how I’d like it to stay:
correto

But this is how it’s getting:
inserir a descrição da imagem aqui

That’s the code I’m using:

var wo = new Array();
while ( listItemEnumerator.moveNext() ) 
        {
            var oListItem = listItemEnumerator.get_current();
            wo.push( { ID: oListItem.get_id(), Status: oListItem.get_item( 'Status' ) } );
        }

I need the Object Array to be built inside the WHILE
Someone can help me?

  • 1

    I did not understand the difference between the expected and the obtained result. Could I explain better? Suggestion: instead of doing console.log(wo), do console.log(JSON.stringify(wo)) and see what the real difference is between the expected and the obtained. Because at first glance, your code seems correct... P.S. Related question

  • looks like he’s building an Object Object instead of Object Array.. I’ll test your command

  • @Thiago, your array seems to be correct, check your result again.

  • 1

    @Thiago Both screenshots show arrays. In one case, the array is very large, so it was represented by Array[32]. On the other, he fits the line, so he was represented by [Object,Object,Object...]. But both are arrays even, internally there is no difference at all. By the way, if you are not familiar with arrays literals, I suggest you read the related question.

  • really has no difference anyway, in both ways this creating an array of objects...

1 answer

1


There is no difference between the two, the difference is in the way the Chrome console is showing it only.

And it’s not because of the size of the Array that it does this, because of the tests I did here it changes the way it displays the array if at the time the console.log is called the development tools are open.

Testing here on version 36 of Chrome, if I have the development tools open and then open the page with Script the result is the second, but if I leave the development tools closed, open the page and only after it runs the script I open the development tools then the result will be the first.

And only one note, the recommended when creating an array in javascript is to use as follows

var wo = [];

You can read a little more of why of this on the w3schools, in the "Avoid new Array()"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.