0

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:

Console 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

elpha01
  • 196
  • 2
  • 4
  • 19
  • The array is empty... – smac89 Mar 23 '18 at 15:03
  • @smac89 No, it has one element. – Cristian Lupascu Mar 23 '18 at 15:04
  • Uh you seems to have right ?! I add console.log(array.length) and it outputs 0. – elpha01 Mar 23 '18 at 15:06
  • Can you please show how you created (or accessed) that `array`? I cannot imagine something that matches `Array.isArray` to come out with curly braces. (Whether it actually has that element already or not [is a separate question](https://stackoverflow.com/q/4057440/1048572)) – Bergi Mar 23 '18 at 15:06
  • 1
    Yea the output you are getting makes no sense with the given input. Make sure something has not hijacked the `stringify` method of `JSON` – smac89 Mar 23 '18 at 15:08
  • You can always try to override the method `toJSON` in your object, and return an associative array of your values. – Nicolas Mar 23 '18 at 15:08
  • 1
    Hint: You could use `<>` button to create a runnable example demonstrating the issue. But the output you get looks pretty much impossible. – Yury Tarabanko Mar 23 '18 at 15:08
  • How do you add that object to the array? – DiabloSteve Mar 23 '18 at 15:10
  • I edited the message, I will try to copy part of the code in an online editor. – elpha01 Mar 23 '18 at 15:12
  • Could you provide more code, such as where does the objects come from, and are they objects or instance of a class. – Nicolas Mar 23 '18 at 15:14
  • How do you create the object? obj = new Object(); obj.name = ... like this? I try it in console and JSON.stringify works correctly for me. – DiabloSteve Mar 23 '18 at 15:17
  • Guessing you might be running into this same issue, or something similar: https://stackoverflow.com/questions/9365624/json-stringify-serializes-to – jmcgriz Mar 23 '18 at 15:23
  • what does `{cameraArray:Array}` do? it is an object not an array. It looks like this is your issue. change it to just `JSON.stringify(cameraArray)` - also, why do you need to stringify it? – George Mar 23 '18 at 15:40
  • @elpha01 Is this typescript? If not, this syntax is all kinds of wrong `{cameraArray:Array};` – jmcgriz Mar 23 '18 at 16:01

2 Answers2

1

You are stringifying an object {}, not the actual array.

let cameraArray = [{myObj:"some content"}];

//wrong
console.log(JSON.stringify({cameraArray:Array}));
console.log("array? ", Array.isArray({cameraArray:Array}));

//right
console.log(JSON.stringify(cameraArray));
console.log("array? ", Array.isArray(cameraArray));
George
  • 2,330
  • 3
  • 15
  • 36
0

I fixed my problem but I absolutely don't know how the output could show this.

the sendCameras call was not at the good position, it has to be at the end of openCamerasByIdFromNames otherwise the sendCameras will be called before push. But I don't understand how could the output show the array with one element.

elpha01
  • 196
  • 2
  • 4
  • 19