0

This is probably a very basic problem, as I'm a javascript beginner.

I've got an object which console.log describes as the following :

enter image description here

As you can see, it holds a certain amount of data.

But when I try to display it as JSON on an HTML page using JSON.stringify, the only thing that displays is this :

[[],[],[],[],[],[],[],[],[]]

Which is very weird, because if instead of JSON.stringify, I do for example this :

div.innerHTML = myObject[0].ArmyName;

....well I get "ARMY_1" to display. So the object is correctly populated. But if I do

div.innerHTML = JSON.stringify(myObject)

or

div.innerHTML = JSON.stringify(myObject[0])

then I only get null / empty data to display.

Am I doing something wrong ?

4
  • 5
    Those are all empty arrays with some custom properties. Arrays in JavaScript always have numerical indexes. Use objects instead. Commented Apr 2, 2018 at 9:47
  • I get this data from another program and I can't really change the way it is made. But can you tell me if I could convert this "custom array" to object ? Commented Apr 2, 2018 at 9:49
  • 1
    It's very likely that you're making a mistake when you retrieve the data. That's where it should be fixed. Commented Apr 2, 2018 at 9:52
  • I guess you are resetting the data of the object/array after you are assigning it to innerHTML. Because objects when assigned are only going to copy the references. Put your code in codepen and see whats happening. Commented Apr 2, 2018 at 9:58

1 Answer 1

2

Just convert your pseudo 2d array into an array of objects:

 array = array.map(el => Object.assign({}, el))

And you should tell the one who created that API that this is not the way to go.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.