0

I have the following code javascript code

var tasksPlayed = [];
tasksPlayed[90] = true;

var json = JSON.stringify(tasksPlayed);

var output = JSON.parse(json);

this gives an error

SyntaxError: JSON.parse

Im not sure why, if I do a .toSource() on the objects I get

keyValue:"[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,true]"})

Any ideas where I might be going wrong

1 Answer 1

1

I don't get this error when I run the code, but the thing with the null values is that tasksPlayed is an array, so when you set tasksPlayed[90] = true;, then it automatically fills all positions from 0 to 89 with null. Try using a plain object instead of an array:

var tasksPlayed = {};
tasksPlayed[90] = true;

var json = JSON.stringify(tasksPlayed);

var output = JSON.parse(json);
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.