从事件源创建对象

时间:2018-11-08 20:01:53

标签: javascript arrays javascript-objects

我正在尝试将日期和数据从事件源存储到包含coreid的对象中,并继续将数据和日期推送到正确的coreid对象。

截至目前,它正在将wifiData存储到两个coreid而不是相应的 <template> <div class="container"> <h2>Probe Diagnostics</h2> <div class="row"> <div class="col"> <line-chart id="wifiChart" ytitle="Signal Strength" label="Wifi Strength" :colors="['#b00']" :messages="{empty: 'Waiting for data'}" :data="wifiData" height="250px" :library="{backgroundColor: '#eee'}" :download="true" :min="-20" :max="20"></line-chart> <column-chart :data="wifiData" ytitle="Signal Strength" height="250px"></column-chart> </div> <div class="col"> <line-chart :data="psSoc" ytitle="ps-soc" height="250px"></line-chart> <line-chart :data="psVoltage" ytitle="ps-voltage" height="250px"></line-chart> </div> </div> </div> </template> <script> let wifiData = []; let psSoc = []; let psVoltage = []; let photons = {}; export default { data() { return { wifiData, psSoc, psVoltage, photons, } }, mounted() { this.streamData(); }, methods: { streamData() { // LIVE PUSH EVENTS if (typeof (EventSource) !== "undefined") { var eventSource = new EventSource( "http://10.10.10.2:8020/v1/Events/?access_token=687b5aee0b82f6536b65f"); eventSource.addEventListener('open', function (e) { console.log("Opened connection to event stream!"); }, false); eventSource.addEventListener('error', function (e) { console.log("Errored!"); }, false); eventSource.addEventListener('WiFi Signal', function (e) { var parsedData = JSON.parse(e.data); if (parsedData.coreid in photons) { photons[parsedData.coreid].push([parsedData.published_at, parsedData.data]) return } else { photons[parsedData.coreid] =[] } }, false); eventSource.addEventListener('ps-soc', function (e) { var parsedData = JSON.parse(e.data); psSoc.push([parsedData.published_at, parsedData.data]) }, false); eventSource.addEventListener('ps-voltage', function (e) { var parsedData = JSON.parse(e.data); psVoltage.push([parsedData.published_at, parsedData.data]) }, false); } } } } </script> 中。如何将数据推送到正确的ID?

for(int i=0;i<myDeque.size()/2;i++)
{
        int a=myDeque.front();
        myDeque.pop_front();
        myDeque.push_back(a);
}

1 个答案:

答案 0 :(得分:1)

完全删除wifiData。而是直接在查找对象内部管理数组:

 // Initialize if needed:
 if(!photons[parsedData.coreid])
   photons[parsedData.coreid] = [];

 // Then push directly to it:
 photons[parsedData.coreid].push(/*...*/);