console.log showing contents of array object

后端 未结 7 1814
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 16:02

I have tried using console.log so I can see the content of my array that contains multiple objects. However I get an error saying console.log is not an

相关标签:
7条回答
  • 2021-01-30 16:42

    The console object is available in Internet Explorer 8 or newer, but only if you open the Developer Tools window by pressing F12 or via the menu.

    It stays available even if you close the Developer Tools window again until you close your IE.

    Chorme and Opera always have an available console, at least in the current versions. Firefox has a console when using Firebug, but it may also provide one without Firebug.

    In any case it is a save approach to make the use of console output optional. Here are some examples on how to do that:

    if (console) {
        console.log('Hello World!');
    }
    
    if (console) console.debug('value of someVar: ' + someVar);
    
    0 讨论(0)
  • 2021-01-30 16:42

    Seems like Firebug or whatever Debugger you are using, is not initialized properly. Are you sure Firebug is fully initialized when you try to access the console.log()-method? Check the Console-Tab (if it's set to activated).

    Another possibility could be, that you overwrite the console-Object yourself anywhere in the code.

    0 讨论(0)
  • 2021-01-30 16:50

    It's simple to print an object to console in Javascript. Just use the following syntax:

    console.log( object );
    

    or

    console.log('object: %O', object );
    

    A relatively unknown method is following which prints an object or array to the console as table:

    console.table( object );

    I think it is important to say that this kind of logging statement only works inside a browser environment. I used this with Google Chrome. You can watch the output of your console.log calls inside the Developer Console: Open it by right click on any element in the webpage and select 'Inspect'. Select tab 'Console'.

    0 讨论(0)
  • 2021-01-30 16:51

    Json stands for JavaScript Object Notation really all json is are javascript objects so your array is in json form already. To write it out in a div you could do a bunch of things one of the easiest I think would be:

     objectDiv.innerHTML = filter;
    

    where objectDiv is the div you want selected from the DOM using jquery. If you wanted to list parts of the array out you could access them since it is a javascript object like so:

     objectDiv.innerHTML = filter.dvals.valueToDisplay; //brand or count depending.
    

    edit: anything you want to be a string but is not currently (which is rare javascript treats almost everything as a string) just use the toString() function built in. so line above if you needed it would be filter.dvals.valueToDisplay.toString();

    second edit to clarify: this answer is in response to the OP's comments and not completely to his original question.

    0 讨论(0)
  • 2021-01-30 16:52

    there are two potential simple solutions to dumping an array as string. Depending on the environment you're using:

    …with modern browsers use JSON:

    JSON.stringify(filters);
    // returns this
    "{"dvals":[{"brand":"1","count":"1"},{"brand":"2","count":"2"},{"brand":"3","count":"3"}]}"
    

    …with something like node.js you can use console.info()

    console.info(filters);
    // will output:
    { dvals: 
    [ { brand: '1', count: '1' },
      { brand: '2', count: '2' },
      { brand: '3', count: '3' } ] }
    

    Edit:

    JSON.stringify comes with two more optional parameters. The third "spaces" parameter enables pretty printing:

    JSON.stringify(
                    obj,      // the object to stringify
                    replacer, // a function or array transforming the result
                    spaces    // prettyprint indentation spaces
                  )
    

    example:

    JSON.stringify(filters, null, "  ");
    // returns this
    "{
     "dvals": [
      {
       "brand": "1",
       "count": "1"
      },
      {
       "brand": "2",
       "count": "2"
      },
      {
       "brand": "3",
       "count": "3"
      }
     ]
    }"
    
    0 讨论(0)
  • 2021-01-30 16:59

    console.log does not produce any message box. I don't think it is available in any version of IE (nor Firefox) without the addition of firebug or some equivalent.

    It is however available in Safari and Chrome. Since you mention Chrome I'll use that for my example.

    You'll need to open your window and its developer window counterpart. you can do this by right clicking any element on the page and selecting "Inspect element". your window will be divided in two parts, the developer part being the bottom. in the division between the two parts is a bar with buttons and the rightmost button there is labeled "console". You'll need to click that to switch to the console tab. Press F12 for developer tools in most browsers on Windows, command + shift + I on macOS.

    Once there, you will be able to interact with whatever page is loaded on top through javascript from that console, and any messages you console.log will be displayed there.

    0 讨论(0)
提交回复
热议问题