1

Some object property/array manipulation. Is there better syntax to accomplish part 2 and 3?

// 1. create object and add element/arrays
var myObject = {};
myObject["123"] = { "A": 123, "B": 456 };  // add 
myObject["123"] = { "C": 123, "D": 456 };  // add more elements to array in element!
myObject["124"] = { "A": 123, "B": 456 };
myObject["125"] = { "A": 123, "B": 456 };
console.log(myObject);

// 2. delete first property in array
for (property in myObject){
        delete myObject[property];
        break;
    }

// 3. show first remaining property
for (property in myObject){
        x = property;
        console.log("first remaining property is " + x );
        break;
    }
Mustapha George
  • 2,497
  • 9
  • 47
  • 79
  • 1
    Property assignments in JavaScript are not additive. You're replacing the complete object held in the key `"123"` - `{ "A": 123, "B": 456 };` in the second line, and not adding more elements - `{ "C": 123, "D": 456 };` to it. – Anurag Dec 30 '11 at 03:38
  • Properties are not guaranteed to iterate in order. Therefore, you shouldn't rely on your code to delete the first property http://stackoverflow.com/questions/5773950/how-to-keep-an-javascript-object-array-ordered-while-also-maintaining-key-lookup – Ruan Mendes Dec 30 '11 at 03:45
  • Actually, I don't really need first property... it is enough to bite off one property at a time, and thanks for alerting that I am replacing elements! – Mustapha George Dec 30 '11 at 03:58

3 Answers3

2

I'm not sure why you're only going half-way with your object literal syntax (JSON mimics object literal declarations), but it's also created a bug for you. You're over-writing myObject["123"] on the second assignment.

You could much more simply write that entire section 1 as:

var myObject = {
    "123": {
        "A": 123,
        "B": 456,
        "C": 123,
        "D": 456
    },
    "124": {
        "A": 123,
        "B": 456
    },
    "125": {
        "A": 123,
        "B": 456
    }
}

Second and third, there is no such thing as "first property in array." This is a pretty common mistake for people who write javascript (not just new people, but people who've been writing it for years).

Under no circumstances what-so-ever is any part of an object ever "First" or "second" or have any order in the object. This is clearly outlined in the ECMA-262 specification. Browser vendors will sometimes accommodate this behaviour which is why "it works" sometimes.

This is because objects are not arrays, nor will they ever be. If you want things to be in order of an array, you need to use an array. Let me ask you, what is the "first" element in the document object? Clearly that's a foolish question, but it proves the point. Objects do not maintain order, that's what arrays do.

So use an array for that. Square brackets denote an array, which does not take a string as a key (that's something objects do). To make things more confusing, arrays are objects, so they can act like objects-- don't confuse that and think objects are arrays.

Incognito
  • 20,537
  • 15
  • 80
  • 120
  • Minor quibble, but it's not really "JSON", it's an object literal. – James Montagne Dec 30 '11 at 03:49
  • JSON is simple a subset of a JS object literal, basically, no functions and you must quote property names. So the above does qualify as JSON, in my book. @JamesMontagne: do you only consider it JSON if it's a string? – Ruan Mendes Dec 30 '11 at 03:52
  • @juan Yes. It's a data interchange format. In this case it's just an object literal. Might just be me. It's my (maybe) strict interpretation of "JSON is a text format that is completely language independent" – James Montagne Dec 30 '11 at 03:55
  • @JamesMontagne You're right, I'll fix that right now. Thanks for the catch. – Incognito Dec 30 '11 at 04:47
1
myObject["123"] = { "C": 123, "D": 456 }; 

does not add more elements to the object (associative array), it replaces them; to add elements you'd have to write:

myObject["123"].C =123;
myObject["123"].D = 456;

As for #2 and #3, Javascript objects do not guarantee to return properties in the order in which they were added; for this you would have to resort to an array, and then after adjusting your data to the strictures of the different data structure, you could get the first element with:

myArray.shift()
Dexygen
  • 12,287
  • 13
  • 80
  • 147
0

You could use Object.keys() in browsers which support it:

console.log("first remaining property is " + Object.keys(myObject)[0] );

Though I'm unsure if order is guaranteed with either approach.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • I don't understand what the OP is asking, or what this answer attempts to answer :p – Ruan Mendes Dec 30 '11 at 03:46
  • The way I understand it, he wants to know if there's a better way to get the "first" property of his object. – James Montagne Dec 30 '11 at 03:48
  • Order is never guaranteed with objects; order is a feature of arrays. – Incognito Dec 30 '11 at 03:49
  • Sounds more like delete any property, and then get any remaining property to me. – Anurag Dec 30 '11 at 03:49
  • @incognito I know conceptually order has no real meaning, just not sure how it's implemented in browsers. Overall, this all seems like a bad idea. – James Montagne Dec 30 '11 at 03:51
  • @JamesMontagne - speaking implementation wise, most browsers do actually support an iteration order on object properties based on insertion order. See this [question](http://stackoverflow.com/questions/3122548/iterating-javascript-object-properties-and-arrays-with-for-in-when-ordering-is) I asked a while back and specifically the section titled `Cross-browser issues` that was taken from an MDC article. – Anurag Dec 30 '11 at 04:13
  • @JamesMontagne Browsers typically support it but there's been known issues, there's also pressure to remove the feature going forward as it tends to make things a bit slower. – Incognito Dec 30 '11 at 04:49