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
javascript
|
show 1 more comment
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
javascript
It's because you push 0 for these indexes (infor
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 alsounshift
to insert at the beginning andpop
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 allDevice_Data
elements or only last 12?
– barbsan
Nov 9 at 13:14
|
show 1 more comment
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
javascript
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
javascript
edited Nov 9 at 12:58
asked Nov 9 at 12:43
XCeptable
57141433
57141433
It's because you push 0 for these indexes (infor
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 alsounshift
to insert at the beginning andpop
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 allDevice_Data
elements or only last 12?
– barbsan
Nov 9 at 13:14
|
show 1 more comment
It's because you push 0 for these indexes (infor
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 alsounshift
to insert at the beginning andpop
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 allDevice_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
|
show 1 more comment
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>
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
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 andpop
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