I am going crazy on Javascript because I can't make JSON.stringify works with a simple array of object. I have searched for similar problem on google and nothing solved my problem.
Here is the code:
console.log(array);
console.log(typeof array);
console.log(Array.isArray(array));
console.log(JSON.stringify(array));
Here the output:
So the first line is correct, this is an array of object. The second line is for me false but it seems to be correct because of Javascript. Third line is correct. Fourth line is incorrect, the array is stringified as an empty object?
I have probably missed a stupid thing but I really can't figure what.
Thank you
EDIT: Here how I globally create the array previously
var array = [];
...
array.push(obj);
EDIT2: Complete code
sendCameras= function(cameraArray){
var message = {};
message.cameras = cameraArray;
console.log("----------------");
console.log(cameraArray);
console.log(typeof cameraArray);
console.log(Array.isArray( cameraArray));
console.log(cameraArray.length);
console.log(JSON.stringify({cameraArray:Array}));
event.source.postMessage(JSON.stringify(message), event.origin);
}
The second part of the code that calls the previous function:
openCamerasByNames= function(cameras){
var cameraToOpen = [];
var openCamerasByIdFromNames =function (){
external_bloc :for (let i = 0; i < cameras.length; i++){
internal_bloc :for (let j = 0; j < cameraList.cameras.length; j++){
if (cameraList.cameras[j].name == cameras[i].name){
openCameraById(cameraList.cameras[j].id);
cameraToOpen.push(cameraList.cameras[j]);
break internal_bloc;
}
}
}
}
sendCameras(cameraToOpen);
...
EDIT3: cameraList is created from JSON.parse