Mustapha George
Mustapha George

Reputation: 2527

javascript object property/array manipulation

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;
    }

Upvotes: 1

Views: 17060

Answers (3)

Incognito
Incognito

Reputation: 20765

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.

Upvotes: 2

Dexygen
Dexygen

Reputation: 12561

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()

Upvotes: 1

James Montagne
James Montagne

Reputation: 78630

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.

Upvotes: 0

Related Questions