3
[
   {"task":"test","created":"/Date(1291676980607)/"},
   {"task":"One More Big Test","created":"/Date(1291677246057)/"},
   {"task":"New Task","created":"/Date(1291747764564)/"}
]

I looked on here, and someone had the same sort of question, but the "checked" correct answer was that it will be different on IE if the item is deleted, which would be fine. My issue is, those items above are stored, but when i go and grab them, iterate, and return, the items are reversed and the created is at the 0 index and task is at 1. Also, i need to return this as JSON.

Here is my basic JS (value == an int the user is passing in):

outputJSON = {};
for(x in json[value]){
    outputJSON[x] = _objectRevival(json[value][x]);
}
return outputJSON;

That returns:

created: Mon Dec 06 2010 15:09:40 GMT-0800 (Pacific Standard Time)
task: "test"
0

4 Answers 4

4

The order of the properties of an object is undefined. It is not possible to force them in a specified order. If you need them in a specific order, you can build this structure reliably using arrays:

var values = [
   [["task", "test"],              ["created", "/Date(1291676980607)/"]],
   [["task", "One More Big Test"], ["created", "/Date(1291677246057)/"]],
   [["task", "New Task"],          ["created", "/Date(1291747764564)/"]]
];

Then you can iterate over your structure like this:

for (var i = 0; i < values.length; i++) {
    for (var k = 0; k < values[i]; k++) {
        // values[i][k][0] contains the label (index 0)
        // values[i][k][1] contains the value (index 1)
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

If you need order, you're going to have to store your data differently, for instance as an array of two-element arrays or as an array of key/value pair objects.
@Matti Virkkunen: I solved that a little different here to save memory.
OP didn't exactly specify whether he knows all the properties in advance or not, or whether the order should indeed follow the data, no matter what order it comes in.
0

To enforce a particular order for your output just replace json[value] in your for loop with an array of the object properties in the order you want to display them, in your case ["task", "created"].

Comments

0

The problem is that javascript objects don't store their properties in a specific order. Arrays on the other do (hence why you can get something consistent from json[0], json[1], json[2]).

If your objects will always have "task" and "created", then you can get at them in any order you want.

json[value]["task"]

and

json[value]["created"]

Update: This should work with your existing code. Before sending the json object:

var before = [
   {"task":"test","created":"/Date(1291676980607)/"},
   {"task":"One More Big Test","created":"/Date(1291677246057)/"},
   {"task":"New Task","created":"/Date(1291747764564)/"}
];
var order = [];
for (var name in before[0]) {
   order.push(name); // puts "task", then "created" into order (for this example)
}

Then send your json off to the server. Later when you get the data back from the server:

var outputJSON = {};
for (var x in order) {
   if (order.hasOwnProperty(x)) {
      outputJSON[order[x]] = _objectRevival(json[value][order[x]]); // I'm not sure what _objectRevival is...do you need it?
   }
}
return outputJSON;

3 Comments

Yeah, i wish, but they wont. Thats just a demo use case. They can store anything.
okay, then before you store the objects, can you push their names into an array, in order to preserve the original order? Then when you get it back, read them out based on the order of the array (iterate over the array, and get json[value][array[i]] ).
_objectRevival() makes it so if some data was stored that was an object, for example, if a user put in a Date() object, it converts it to .getTime() then puts it in /Date(12345...)/ and _objectRevival() makes it back into a Date() or other object, or, if it matches no predefined objects, just what i gave it back.
0
var items = ["bag", "book", "pen", "car"];
items.reverse();

This will result in the following output:

car , pen, book, bag

Even if you have JSON array it will reverse.

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.