2017-06-14 109 views
-2

我们试图用for循环创建一个json对象。结果应该是这样的:如何将元素插入到JSON格式的Javascript对象中

posJSON = [ 
    { 
     "position": [msg[0].Longitude, msg[0].Latitude], 
     "radius": 0.05, 
     "color": [255, 255, 0, 255] 
    }, 
    { 
     "position": [msg[1].Longitude, msg[1].Latitude], 
     "radius": 0.05, 
     "color": [255, 0, 0, 255] 
    } 
] 

如果我们只是用创建对象的代码^一切工作正常,但如果我们试图dinamically创建这样的对象:

for(i=0; i < Object.keys(msg).length; i++) { 
    posJSON.push({ 
     "position": [msg[i].Longitude, msg[i].Latitude], 
     "radius": 0.05, 
     "color": [255, 255, 0, 255] 
    }); 
} 

它赢得没有工作。我们显然使用了错误的语法,但我们无法理解错误在哪里。 先谢谢任何能回答我们问题的人。

这是函数的全码:

import React, {Component} from 'react'; 
import DeckGL, {PathLayer} from 'deck.gl'; 
import DeckGL, {ScatterplotLayer} from 'deck.gl'; 
import $ from 'jquery'; 

var tableJSONStringed = null; 
var posJSON = []; 

//Position tracker 

window.setInterval(function(){ 
    var request = $.ajax({ 
     url: "https://nedo93.000webhostapp.com/phpgetcoords.php",     
     method: "POST",    
     dataType: "json"  
    }); 
    request.done(function(msg) {    
     tableJSONStringed = JSON.stringify(msg, null, " "); 
     var count = Object.keys(msg).length;    

     //CYCLE 
     for(i=0; i < Object.keys(msg).length; i++) { 
     posJSON.push({ 
     "position": [msg[i].Longitude, msg[i].Latitude], 
     "radius": 0.05, 
     "color": [255, 255, 0, 255] 
    }); 



     /*posJSON = [ 
      { 
       "position": [msg[0].Longitude, msg[0].Latitude], 
       "radius": 0.05, 
       "color": [255, 255, 0, 255] 
      }, 
      { 
       "position": [msg[1].Longitude, msg[1].Latitude], 
       "radius": 0.05, 
       "color": [255, 0, 0, 255] 
      } 
     ];*/ 

    });    
    request.fail(function(jqXHR, textStatus) { 
     //alert("Request failed: " + textStatus); 
    }); 
}, 5000); 

export default class DeckGLOverlay extends Component { 

    static get defaultViewport() { 
return { 
    longitude: 11.25, 
    latitude: 43.77, 
    zoom: 16, 
    maxZoom: 18, 
    pitch: 40, //viewport tilt 
    bearing: 0 
}; 
    } 

    _initialize(gl) { 
gl.enable(gl.DEPTH_TEST); 
gl.depthFunc(gl.LEQUAL); 
} 

//Add in const { }, new layers's ID 
    render() { 
const {viewport, scatters, pos, icon, paths, trailLength, time} = this.props; 

//Add another instance of a new layer here: 

const layers = [  
    new ScatterplotLayer({ 
    id: 'scatters', 
    data: scatters,  
    radiusScale: 100, 
    outline: false 
    }), 
    new ScatterplotLayer({ 
    id: 'pos', 
    data: posJSON,  
    radiusScale: 100, 
    outline: false 
    }),  
    new PathLayer({ 
    id: 'paths', 
    data: paths,   
    rounded: true, 
    getColor: paths => [255, 0, 0, 255], 
    getWidth: paths => 0.03, 
    widthScale: 100 
    }) 
]; 

return (
    <DeckGL {...viewport} layers={layers} onWebGLInitialized={this._initialize} /> 
);  
    }  
} 

在这里它是运行的周期的控制台输出:Image

+3

请解释一下你的“这不会是什么意思工作”。它有什么作用? javascript控制台是否提供错误? – Soviut

+0

什么是味精?它对于 –

+0

这个问题很重要,你能告诉我们味精吗? –

回答

0
var posJSON = []; 
var msg = [ 
    {Longitude: 1, Latitude:11, radius: 0.01, color: [221,2,32,1]}, 
    {Longitude: 2, Latitude:21, radius: 0.11, color: [321,2,23,1]}, 
    {Longitude: 3, Latitude:31, radius: 0.41, color: [521,2,22,1]}, 
    {Longitude: 4, Latitude:41, radius: 0.11, color: [121,2,02,1]}, 
    {Longitude: 5, Latitude:51, radius: 0.32, color: [253,2,22,1]}, 
] 
msg.forEach(function(obj) { 
    posJSON.push({ 
     "position": [obj.Longitude, obj.Latitude], 
     "radius": obj.radius, 
     "color": obj.color 
    }); 
}); 

console.log(posJSON); 
+0

这不适用于他的问题示例代码的上下文中。这是同步代码,由于AJAX调用,他的代码是异步的。阅读评论;他可以很好地填充数据,所以你的循环逻辑是多余的。事实上,他的绘图函数在他的数据被填充之前被调用,而这正在给出错误。 – Lansana

+0

这并不重要,因为一旦异步调用完成,他就会收到“msg”对象。 – AnoopGoudar

+0

这很重要......如果你在'msg'尚未填充的时候调用'msg'对象的'myFunc',那么当响应返回时,你可以填充'msg',但是没有任何事情会发生,因为你已经被称为功能。要么你必须第二次调用该函数,要么延迟调用它直到响应返回,并在响应结果中填充“msg”。这就是异步编程的工作原理。 – Lansana

相关问题