1

I have the following code :

$.each( orderedqty, function( priority, quantity ) {

    // for each priority

    if ( $(".switch[data-priorityid=" + priority + "]").attr("data-state") == "on" ) {

        // this priority is checked

        orderedqtyArray[ priority ] = 15;

    } else {

        orderedqtyArray[ priority ] = 10;

    }

});

console.info( orderedqtyArray );
console.info( JSON.stringify( orderedqtyArray ) );

The problem is this code returns :

[77: 15, 78: 15]
[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,15,15]

[78: 15]
[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,15]

Instead of a beautiful json :(

5
  • So you're creating a sparce array and when you convert it to JSON you get … a sparce array. What's the problem? Commented Oct 8, 2014 at 9:46
  • orderedqty looks like ? can you share it Commented Oct 8, 2014 at 9:46
  • it's a beautiful json, everything is normal :) Commented Oct 8, 2014 at 9:48
  • If you're referring to the fact that the stringify output lacks the keys "77: " & "78: " this will be due to the fact that they're integers and will be taken as array indices. Commented Oct 8, 2014 at 9:51
  • You can replace null with an empty string if you aren't expecting null in the string result. JSON.stringify(orderedqtyArray).replace(/null/g, "") Commented Oct 8, 2014 at 9:56

3 Answers 3

6

The browser's console.log output is optimized to remove the nulls, but it's the same array. JSON can't do this, it always includes all the nulls in the array.

If you want the stringified output to look more like console.log's, then you need to define it as an object and not as an array.

var a = [], b = {};
a[5] = 1;
b[5] = 1;
console.log(JSON.stringify(a));
console.log(JSON.stringify(b));

The output will be

[null, null, null, null, null, 1]
{"5": 1}
Sign up to request clarification or add additional context in comments.

Comments

3

Something strange. But you should be aware of interesting behaviour of arrays in Js.

If you define something like this:

var array = [];
//defining item at arbitrary position
array[5] = 5;


// output from array will look something like this
 [undefined, undefined, undefined, undefined, 5]

So perhaps your orderedqtyArray[ priority ] sets value at some random index and it leads to the problem. Because if you would have normal array it would definitely stringify into valid json.

2 Comments

+1, please see my comment above also - you're using integers as your keys in orderedqtyArray, therefore making JavaScript think that you want an array (or maybe you do).
sure it might be an Object for sure.
2

If you just want to remove the null values, you can do :

JSON.stringify( orderedqtyArray.filter(function(e){return e}));

1 Comment

But it makes no sense, we should avoid getting of these null values.

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.