最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript object propertyarray manipulation - Stack Overflow

programmeradmin9浏览0评论

Some object property/array manipulation. Is there better syntax to acplish 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;
    }

Some object property/array manipulation. Is there better syntax to acplish 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;
    }
Share Improve this question asked Dec 30, 2011 at 3:33 Mustapha GeorgeMustapha George 2,53710 gold badges49 silver badges87 bronze badges 3
  • 1 Property assignments in JavaScript are not additive. You're replacing the plete 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 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./questions/5773950/… – Ruan Mendes 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! – Mustapha George Commented Dec 30, 2011 at 3:58
Add a ment  | 

3 Answers 3

Reset to default 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 mon 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 acmodate 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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论