Array is not fully initialized with data extracted from json array using PUSH & SHIFT











up vote
0
down vote

favorite












I am trying to make two arrays by extracting data from a json array using Push & Shift method. The json file is updated with new data periodically. But I am not able to populate the arrays with all & updated data. It is always last index[11] being populated. Rest 0-11 are always zero.



How to correct it.



Here is my code:



 <script>
var Device_Data;
var rssi, batt;

function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
var N = 12;
for (i = 0; i < N; i++) {
rssiArray.push(0);
battArray.push(0); }

//console.log(Device_Data[0].rssi);
//console.log(Device_Data[1].battery_voltage_mv);
rssi = Device_Data[0].rssi;
batt = Device_Data[1].battery_voltage_mv;
rssiArray.shift();
rssiArray.push(rssi);
battArray.shift();
battArray.push(batt);

console.log(rssiArray);
console.log(battArray);

</script>


Output of console.log(Device_Data);



288) […]
​[0…99]
​​0: {…}
​​​battery_voltage_mv: 131
​​​rssi: "-110"
​​​<prototype>: Object { … }
​​1: Object { rssi: "-134", battery_voltage_mv: 131 }
​​2: Object { rssi: "-125", battery_voltage_mv: 131 }
​​3: Object { rssi: "-132", battery_voltage_mv: 131 }


Output of console.log(rssiArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: "-110"


Output of console.log(battArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: 131









share|improve this question
























  • It's because you push 0 for these indexes (in for loop)
    – barbsan
    Nov 9 at 13:02












  • Yes, can you tell how do I populate it fully ?
    – XCeptable
    Nov 9 at 13:04












  • maybe you were thinking the functions were working in the other direction, look also unshift to insert at the beginning and pop to remove at the end
    – Nahuel Fouilleul
    Nov 9 at 13:08










  • Yes, I need both pop & unshift OR shift, as one record come each update & one at the end need to be removed.
    – XCeptable
    Nov 9 at 13:11










  • I could, but I don't fully get your intention... Are you going to map all Device_Data elements or only last 12?
    – barbsan
    Nov 9 at 13:14















up vote
0
down vote

favorite












I am trying to make two arrays by extracting data from a json array using Push & Shift method. The json file is updated with new data periodically. But I am not able to populate the arrays with all & updated data. It is always last index[11] being populated. Rest 0-11 are always zero.



How to correct it.



Here is my code:



 <script>
var Device_Data;
var rssi, batt;

function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
var N = 12;
for (i = 0; i < N; i++) {
rssiArray.push(0);
battArray.push(0); }

//console.log(Device_Data[0].rssi);
//console.log(Device_Data[1].battery_voltage_mv);
rssi = Device_Data[0].rssi;
batt = Device_Data[1].battery_voltage_mv;
rssiArray.shift();
rssiArray.push(rssi);
battArray.shift();
battArray.push(batt);

console.log(rssiArray);
console.log(battArray);

</script>


Output of console.log(Device_Data);



288) […]
​[0…99]
​​0: {…}
​​​battery_voltage_mv: 131
​​​rssi: "-110"
​​​<prototype>: Object { … }
​​1: Object { rssi: "-134", battery_voltage_mv: 131 }
​​2: Object { rssi: "-125", battery_voltage_mv: 131 }
​​3: Object { rssi: "-132", battery_voltage_mv: 131 }


Output of console.log(rssiArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: "-110"


Output of console.log(battArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: 131









share|improve this question
























  • It's because you push 0 for these indexes (in for loop)
    – barbsan
    Nov 9 at 13:02












  • Yes, can you tell how do I populate it fully ?
    – XCeptable
    Nov 9 at 13:04












  • maybe you were thinking the functions were working in the other direction, look also unshift to insert at the beginning and pop to remove at the end
    – Nahuel Fouilleul
    Nov 9 at 13:08










  • Yes, I need both pop & unshift OR shift, as one record come each update & one at the end need to be removed.
    – XCeptable
    Nov 9 at 13:11










  • I could, but I don't fully get your intention... Are you going to map all Device_Data elements or only last 12?
    – barbsan
    Nov 9 at 13:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to make two arrays by extracting data from a json array using Push & Shift method. The json file is updated with new data periodically. But I am not able to populate the arrays with all & updated data. It is always last index[11] being populated. Rest 0-11 are always zero.



How to correct it.



Here is my code:



 <script>
var Device_Data;
var rssi, batt;

function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
var N = 12;
for (i = 0; i < N; i++) {
rssiArray.push(0);
battArray.push(0); }

//console.log(Device_Data[0].rssi);
//console.log(Device_Data[1].battery_voltage_mv);
rssi = Device_Data[0].rssi;
batt = Device_Data[1].battery_voltage_mv;
rssiArray.shift();
rssiArray.push(rssi);
battArray.shift();
battArray.push(batt);

console.log(rssiArray);
console.log(battArray);

</script>


Output of console.log(Device_Data);



288) […]
​[0…99]
​​0: {…}
​​​battery_voltage_mv: 131
​​​rssi: "-110"
​​​<prototype>: Object { … }
​​1: Object { rssi: "-134", battery_voltage_mv: 131 }
​​2: Object { rssi: "-125", battery_voltage_mv: 131 }
​​3: Object { rssi: "-132", battery_voltage_mv: 131 }


Output of console.log(rssiArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: "-110"


Output of console.log(battArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: 131









share|improve this question















I am trying to make two arrays by extracting data from a json array using Push & Shift method. The json file is updated with new data periodically. But I am not able to populate the arrays with all & updated data. It is always last index[11] being populated. Rest 0-11 are always zero.



How to correct it.



Here is my code:



 <script>
var Device_Data;
var rssi, batt;

function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
var N = 12;
for (i = 0; i < N; i++) {
rssiArray.push(0);
battArray.push(0); }

//console.log(Device_Data[0].rssi);
//console.log(Device_Data[1].battery_voltage_mv);
rssi = Device_Data[0].rssi;
batt = Device_Data[1].battery_voltage_mv;
rssiArray.shift();
rssiArray.push(rssi);
battArray.shift();
battArray.push(batt);

console.log(rssiArray);
console.log(battArray);

</script>


Output of console.log(Device_Data);



288) […]
​[0…99]
​​0: {…}
​​​battery_voltage_mv: 131
​​​rssi: "-110"
​​​<prototype>: Object { … }
​​1: Object { rssi: "-134", battery_voltage_mv: 131 }
​​2: Object { rssi: "-125", battery_voltage_mv: 131 }
​​3: Object { rssi: "-132", battery_voltage_mv: 131 }


Output of console.log(rssiArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: "-110"


Output of console.log(battArray);



(12) […]
​0: 0
​1: 0
​2: 0
​3: 0
​4: 0
​5: 0
​6: 0
​7: 0
​8: 0
​9: 0
​10: 0
​11: 131






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 12:58

























asked Nov 9 at 12:43









XCeptable

57141433




57141433












  • It's because you push 0 for these indexes (in for loop)
    – barbsan
    Nov 9 at 13:02












  • Yes, can you tell how do I populate it fully ?
    – XCeptable
    Nov 9 at 13:04












  • maybe you were thinking the functions were working in the other direction, look also unshift to insert at the beginning and pop to remove at the end
    – Nahuel Fouilleul
    Nov 9 at 13:08










  • Yes, I need both pop & unshift OR shift, as one record come each update & one at the end need to be removed.
    – XCeptable
    Nov 9 at 13:11










  • I could, but I don't fully get your intention... Are you going to map all Device_Data elements or only last 12?
    – barbsan
    Nov 9 at 13:14


















  • It's because you push 0 for these indexes (in for loop)
    – barbsan
    Nov 9 at 13:02












  • Yes, can you tell how do I populate it fully ?
    – XCeptable
    Nov 9 at 13:04












  • maybe you were thinking the functions were working in the other direction, look also unshift to insert at the beginning and pop to remove at the end
    – Nahuel Fouilleul
    Nov 9 at 13:08










  • Yes, I need both pop & unshift OR shift, as one record come each update & one at the end need to be removed.
    – XCeptable
    Nov 9 at 13:11










  • I could, but I don't fully get your intention... Are you going to map all Device_Data elements or only last 12?
    – barbsan
    Nov 9 at 13:14
















It's because you push 0 for these indexes (in for loop)
– barbsan
Nov 9 at 13:02






It's because you push 0 for these indexes (in for loop)
– barbsan
Nov 9 at 13:02














Yes, can you tell how do I populate it fully ?
– XCeptable
Nov 9 at 13:04






Yes, can you tell how do I populate it fully ?
– XCeptable
Nov 9 at 13:04














maybe you were thinking the functions were working in the other direction, look also unshift to insert at the beginning and pop to remove at the end
– Nahuel Fouilleul
Nov 9 at 13:08




maybe you were thinking the functions were working in the other direction, look also unshift to insert at the beginning and pop to remove at the end
– Nahuel Fouilleul
Nov 9 at 13:08












Yes, I need both pop & unshift OR shift, as one record come each update & one at the end need to be removed.
– XCeptable
Nov 9 at 13:11




Yes, I need both pop & unshift OR shift, as one record come each update & one at the end need to be removed.
– XCeptable
Nov 9 at 13:11












I could, but I don't fully get your intention... Are you going to map all Device_Data elements or only last 12?
– barbsan
Nov 9 at 13:14




I could, but I don't fully get your intention... Are you going to map all Device_Data elements or only last 12?
– barbsan
Nov 9 at 13:14












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










What your code those:



filling the arrays rssiArray and battArray with 12 zeros.



Then you are removing the first element with .shift().



Now you have 11 zeros in your arrays.



Then you are adding one element to each array.



So you will have two arrays with the first 11 elements as zeros, then you get one element on position 11 which comes from the Device_Data object.



if you want to add elements to the first position in your arrays then you can use:



rssiArray.unshift(rssi);
battArray.unshift(batt);


The dynamic values will be added as the first elements to the arrays.



But i guess you want to copy the whole structure from Device_Data to your arrays?
Then you can do something like this:



 <script>
function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
for(var i=0; i<Device_Data.length; i++) {
rssiArray.push(Device_Data[i].rssi);
battArray.push(Device_Data[i].battery_voltage_mv);
}

console.log(rssiArray);
console.log(battArray);
}
</script>





share|improve this answer





















  • Yes, copy whole structure at first, & then with each update/dynamic data only last one index is to be update as one new record come with each update.
    – XCeptable
    Nov 9 at 13:08












  • If you want to override the last element in one of your elements you can first pop then push, or do like this rssiArray[rssiArray.length - 1 ] = Device_Data[Device_Data.length - 1].rssi If you want to copy the last element from Device_Data to your array.
    – askepan
    Nov 9 at 13:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225947%2farray-is-not-fully-initialized-with-data-extracted-from-json-array-using-push%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










What your code those:



filling the arrays rssiArray and battArray with 12 zeros.



Then you are removing the first element with .shift().



Now you have 11 zeros in your arrays.



Then you are adding one element to each array.



So you will have two arrays with the first 11 elements as zeros, then you get one element on position 11 which comes from the Device_Data object.



if you want to add elements to the first position in your arrays then you can use:



rssiArray.unshift(rssi);
battArray.unshift(batt);


The dynamic values will be added as the first elements to the arrays.



But i guess you want to copy the whole structure from Device_Data to your arrays?
Then you can do something like this:



 <script>
function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
for(var i=0; i<Device_Data.length; i++) {
rssiArray.push(Device_Data[i].rssi);
battArray.push(Device_Data[i].battery_voltage_mv);
}

console.log(rssiArray);
console.log(battArray);
}
</script>





share|improve this answer





















  • Yes, copy whole structure at first, & then with each update/dynamic data only last one index is to be update as one new record come with each update.
    – XCeptable
    Nov 9 at 13:08












  • If you want to override the last element in one of your elements you can first pop then push, or do like this rssiArray[rssiArray.length - 1 ] = Device_Data[Device_Data.length - 1].rssi If you want to copy the last element from Device_Data to your array.
    – askepan
    Nov 9 at 13:26















up vote
0
down vote



accepted










What your code those:



filling the arrays rssiArray and battArray with 12 zeros.



Then you are removing the first element with .shift().



Now you have 11 zeros in your arrays.



Then you are adding one element to each array.



So you will have two arrays with the first 11 elements as zeros, then you get one element on position 11 which comes from the Device_Data object.



if you want to add elements to the first position in your arrays then you can use:



rssiArray.unshift(rssi);
battArray.unshift(batt);


The dynamic values will be added as the first elements to the arrays.



But i guess you want to copy the whole structure from Device_Data to your arrays?
Then you can do something like this:



 <script>
function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
for(var i=0; i<Device_Data.length; i++) {
rssiArray.push(Device_Data[i].rssi);
battArray.push(Device_Data[i].battery_voltage_mv);
}

console.log(rssiArray);
console.log(battArray);
}
</script>





share|improve this answer





















  • Yes, copy whole structure at first, & then with each update/dynamic data only last one index is to be update as one new record come with each update.
    – XCeptable
    Nov 9 at 13:08












  • If you want to override the last element in one of your elements you can first pop then push, or do like this rssiArray[rssiArray.length - 1 ] = Device_Data[Device_Data.length - 1].rssi If you want to copy the last element from Device_Data to your array.
    – askepan
    Nov 9 at 13:26













up vote
0
down vote



accepted







up vote
0
down vote



accepted






What your code those:



filling the arrays rssiArray and battArray with 12 zeros.



Then you are removing the first element with .shift().



Now you have 11 zeros in your arrays.



Then you are adding one element to each array.



So you will have two arrays with the first 11 elements as zeros, then you get one element on position 11 which comes from the Device_Data object.



if you want to add elements to the first position in your arrays then you can use:



rssiArray.unshift(rssi);
battArray.unshift(batt);


The dynamic values will be added as the first elements to the arrays.



But i guess you want to copy the whole structure from Device_Data to your arrays?
Then you can do something like this:



 <script>
function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
for(var i=0; i<Device_Data.length; i++) {
rssiArray.push(Device_Data[i].rssi);
battArray.push(Device_Data[i].battery_voltage_mv);
}

console.log(rssiArray);
console.log(battArray);
}
</script>





share|improve this answer












What your code those:



filling the arrays rssiArray and battArray with 12 zeros.



Then you are removing the first element with .shift().



Now you have 11 zeros in your arrays.



Then you are adding one element to each array.



So you will have two arrays with the first 11 elements as zeros, then you get one element on position 11 which comes from the Device_Data object.



if you want to add elements to the first position in your arrays then you can use:



rssiArray.unshift(rssi);
battArray.unshift(batt);


The dynamic values will be added as the first elements to the arrays.



But i guess you want to copy the whole structure from Device_Data to your arrays?
Then you can do something like this:



 <script>
function dspChrt(Device_Data) {
console.log(Device_Data);

var rssiArray = ;
var battArray = ;
for(var i=0; i<Device_Data.length; i++) {
rssiArray.push(Device_Data[i].rssi);
battArray.push(Device_Data[i].battery_voltage_mv);
}

console.log(rssiArray);
console.log(battArray);
}
</script>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 13:05









askepan

412




412












  • Yes, copy whole structure at first, & then with each update/dynamic data only last one index is to be update as one new record come with each update.
    – XCeptable
    Nov 9 at 13:08












  • If you want to override the last element in one of your elements you can first pop then push, or do like this rssiArray[rssiArray.length - 1 ] = Device_Data[Device_Data.length - 1].rssi If you want to copy the last element from Device_Data to your array.
    – askepan
    Nov 9 at 13:26


















  • Yes, copy whole structure at first, & then with each update/dynamic data only last one index is to be update as one new record come with each update.
    – XCeptable
    Nov 9 at 13:08












  • If you want to override the last element in one of your elements you can first pop then push, or do like this rssiArray[rssiArray.length - 1 ] = Device_Data[Device_Data.length - 1].rssi If you want to copy the last element from Device_Data to your array.
    – askepan
    Nov 9 at 13:26
















Yes, copy whole structure at first, & then with each update/dynamic data only last one index is to be update as one new record come with each update.
– XCeptable
Nov 9 at 13:08






Yes, copy whole structure at first, & then with each update/dynamic data only last one index is to be update as one new record come with each update.
– XCeptable
Nov 9 at 13:08














If you want to override the last element in one of your elements you can first pop then push, or do like this rssiArray[rssiArray.length - 1 ] = Device_Data[Device_Data.length - 1].rssi If you want to copy the last element from Device_Data to your array.
– askepan
Nov 9 at 13:26




If you want to override the last element in one of your elements you can first pop then push, or do like this rssiArray[rssiArray.length - 1 ] = Device_Data[Device_Data.length - 1].rssi If you want to copy the last element from Device_Data to your array.
– askepan
Nov 9 at 13:26


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225947%2farray-is-not-fully-initialized-with-data-extracted-from-json-array-using-push%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff