0
/* Helper function to clean up any current data we have stored */
function insertSerializedData(ids, type) {
    // Get anything in the current field
    current_data = $('#changes').val();
    if (!current_data) {
        var data = new Array();
        data[type] = ids;
        $('#changes').val(JSON.stringify(data));
    } else {
        var data = JSON.parse($('#changes').val());
        data[type] = ids;
        $('#changes').val(JSON.stringify(data));
    }
    console.log($('#changes').val());
}

I am using the following function to either add data to a current JSON object or create a new JSON object all together to be used in PHP later. Is the stringify() method only for FF? I am using google chrome and I am being given an empty object when using the conosole.log() function...

Also what happens if you try to store two values with the same key? I assume it will overwrite...so I should add a random math number at the end array in order to keep duplicates from showing up?

Thanks :)

5
  • 1
    JSON.stringify does exist in Chrome and IE 8 and later. Your error is somehwere else. Try to call console.log(data) before calling JSON.stringify Commented Jun 22, 2012 at 0:14
  • random numbers don't guarantee uniqueness, they just reduce the probability that it will happen. If it's important, you should track your indexes and increase them as you go, to guarantee unique field names. Commented Jun 22, 2012 at 0:14
  • Here's a useful compatibility table for JSON parsing support: caniuse.com/json Commented Jun 22, 2012 at 0:15
  • Your line that says var data = new Array() should be = new Object(), or even prettier = {}; An array is an ordered list, not a map, it's not PHP. Commented Jun 22, 2012 at 0:16
  • Also, don't ask two questions in one here at SO. You can easily verify that if you store two values with the same key, it will get overwritten. Commented Jun 22, 2012 at 0:18

2 Answers 2

2

These lines may cause problems:

var data = new Array();
data[type] = ids;

... because arrays in JavaScript are not quite like arrays in PHP. I suppose what you meant is better expressed by...

var data = {};
data[type] = ids;

Besides, current_data seems to be local to this function, therefore it also should be declared as local with var. Don't see any other problems... except that similar functionality is already implemented in jQuery .data() method.

UPDATE: here's jsFiddle to play with. ) From what I've tried looks like the array-object mismatch is what actually caused that Chrome behavior.

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

1 Comment

These are all valid comments, but none of them is an answer to the question.
0

I reformatted it a bit, but and this seems to work. It will set the "value" attribute of the #changes element to a JSON string. I assume that the type argument is supposed to be the index of the array which you're trying to assign?

function insertSerializedData(ids, type) {
  var changes = jQuery('#changes'), arr, val = changes.val();
  if (!val) {
    arr = [];
    arr[type] = ids;
    changes.val(JSON.stringify(arr));
  } else {
    arr = JSON.parse(val);
    arr[type] = ids;
    changes.val(JSON.stringify(arr));
  }
  console.log(changes);
}

2 Comments

It would be a waste of memory using type as index, see. ) When var x = []; x[99] = 99; then x.length will be 100, even though the array is efficiently storing just a single element.
I feel that my solution functionally works, but I think you're right that it's sub-optimal. However, it's a drop in the bucket to have 100 items in an array.

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.