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;
    }
3
  • 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. Commented Dec 30, 2011 at 3:38
  • Properties are not guaranteed to iterate in order. Therefore, you shouldn't rely on your code to delete the first property stackoverflow.com/questions/5773950/… Commented Dec 30, 2011 at 3: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! Commented Dec 30, 2011 at 3:58

3 Answers 3

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.

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

4 Comments

Minor quibble, but it's not really "JSON", it's an object literal.
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?
@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"
@JamesMontagne You're right, I'll fix that right now. Thanks for the catch.
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()

Comments

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.

7 Comments

I don't understand what the OP is asking, or what this answer attempts to answer :p
The way I understand it, he wants to know if there's a better way to get the "first" property of his object.
Order is never guaranteed with objects; order is a feature of arrays.
Sounds more like delete any property, and then get any remaining property to me.
@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.
|

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.