简体   繁体   English

直到console.log展开后才能解析Javascript对象键

[英]Javascript Object Keys Not Resolved Until After console.log Is Expanded

I am having the same issue as this stackoverflow question . 我有与此stackoverflow问题相同的问题 While that question was answered, a resolution was not provided. 在回答该问题的同时,未提供解决方案。

I have a javascript object where console.log(anObject) shows all of the keys, but console.log(JSON.stringify(anObject)) does not show them all, it only shows 1 of the 2. 我有一个javascript对象,其中console.log(anObject)显示所有键,但是console.log(JSON.stringify(anObject))并没有全部显示,仅显示2个键中的1个。

What is the cause of this at the javascript (not console) level and how can I resolve this so that the code that follows this segment can access all of the keys? 在javascript(而非控制台)级别导致此错误的原因是什么,如何解决此问题,以便该段之后的代码可以访问所有键?

Is there a way to force object resolution? 有办法强制对象解析吗?

Thanks in advance. 提前致谢。


It is wrapped inside of a getJson which is asynchronous, but ironically the data being left out is not the json result data. 它包装在异步的getJson内部,但具有讽刺意味的是,遗漏的数据不是json结果数据。 I'm using the cytoscape.js library. 我正在使用cytoscape.js库。 The problem occurs around line 5183 of the cytoscape.js file in this section where it is parsing my xar (elements array) as shown below. 该问题在本节中的cytoscape.js文件的第5183行附近发生,在该行中解析我的xar(元素数组),如下所示。 The data key is there, but not the group key unless it is expended in console. 数据密钥在那里,但组密钥不存在,除非在控制台中已用完。

      // specify via opts.nodes and opts.edges
  else if( $$.is.plainObject(opts) && ($$.is.array(opts.nodes) || $$.is.array(opts.edges)) ){
    var elesByGroup = opts;
    var jsons = [];

    var grs = ['nodes', 'edges'];
    for( var i = 0, il = grs.length; i < il; i++ ){
      var group = grs[i];
      var elesArray = elesByGroup[group];

      if( $$.is.array(elesArray) ){

        for( var j = 0, jl = elesArray.length; j < jl; j++ ){
              //console.log(JSON.stringify(elesArray[j]))
          if ( typeof elesArray[j] !== "undefined" ) {
              var json = elesArray[j];
              console.log(elesArray[j].data)
              json.group = group;
              jsons.push( json );
          }
        }
      }
    }

 $(document).ready(function() {

 function notInArray(value, array) {
     return array.indexOf(value) == -1;
 }

 var node_array = new Array();
 var edge_array = new Array();

 var full_data;

 var xar = new Object();

 xar.nodes = [];
 xar.edges = [];

 $.getJSON("http://127.0.0.1/loctest", function(xad) {
     full_data = xad;
     var i = 0;
     $.each(xad, function(key, val) {
         s = val.src;
         d = val.dst;
         if (notInArray(s, node_array)) {
             node_array.push(s);
             xar.nodes[i] = {
                 data: {
                     id: s
                 }
             };
             i++;
         }
         if (notInArray(d, node_array)) {
             node_array.push(d);
             xar.nodes[i] = {
                 data: {
                     id: d
                 }
             };
         }
         xar.edges[i] = {
             data: {
                 id: i.toString(),
                 weight: 3,
                 source: s,
                 target: d
             }
         };
         i++;
     });

     //console.log(xar);
     $('#cy').cytoscape({
         //container: document.getElementById('cy'),

         style: cytoscape.stylesheet()
             .selector('node')
             .css({
                 'content': 'data(id)'
             })
             .selector('edge')
             .css({
                 'target-arrow-shape': 'triangle',
                 'width': 4,
                 'line-color': '#ddd',
                 'target-arrow-color': '#ddd'
             })
             .selector('.highlighted')
             .css({
                 'background-color': '#61bffc',
                 'line-color': '#61bffc',
                 'target-arrow-color': '#61bffc',
                 'transition-property': 'background-color, line-color, target-arrow-color',
                 'transition-duration': '0.5s'
             }),

         elements: xar,

         layout: {
             name: 'breadthfirst',
             directed: true,
             roots: '#a',
             padding: 10
         }
     });


     console.log(cy);
     var bfs = cy.elements().bfs('#a', function() {}, true);

     var p = 0;
     var highlightNextEle = function() {
         bfs.path[p].addClass('highlighted');

         if (p < bfs.path.length) {
             p++;
             setTimeout(highlightNextEle, 1000);
         }
     };

     // kick off first highlight
     highlightNextEle();

 });

});

JSON.stringify only lists enumerable own properties. JSON.stringify仅列出可枚举的自身属性。

This is explained in 15.12.3 : 15.12.3对此进行了解释:

The abstract operation JO ( value ) serializes an object. 抽象操作JOvalue )序列化一个对象。

Let K be an internal List of Strings consisting of the names of all the own properties of value whose [[Enumerable]] attribute is true . K为内部字符串列表,其中包含[[Enumerable]]属性为truevalue的所有自身属性的名称。 The ordering of the Strings should be the same as that used by the Object.keys standard built-in function. 字符串的顺序应与Object.keys标准内置函数所使用的顺序相同。

So if you don't see the property, it can mean 因此,如果您没有看到该物业,则可能意味着

  • The property has not been added yet. 该属性尚未添加。 Note console.log may be asynchroneous, so the property might be added after calling log but be displayed in the console 注意console.log可能是异步的,因此该属性可能在调用log之后添加,但会显示在控制台中
  • The property is inherited instead of own 该属性是继承的,而不是自己的
  • The property is not enumerable 该属性不可枚举

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM