javascript 对象属性/数组操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8675893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript object property/array manipulation
提问by Mustapha George
Some object property/array manipulation. Is there better syntax to accomplish part 2 and 3?
一些对象属性/数组操作。是否有更好的语法来完成第 2 部分和第 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;
}
回答by Incognito
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.
我不确定您为什么只使用对象字面量语法(JSON 模仿对象字面量声明),但它也为您创建了一个错误。您在第二个作业中覆盖了 myObject["123"] 。
You could much more simply write that entire section 1 as:
您可以更简单地将整个第 1 部分写为:
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).
第二和第三,没有“数组中的第一个属性”这样的东西。对于编写 javascript 的人(不仅是新人,而且是已经编写多年的人)来说,这是一个非常常见的错误。
Under no circumstanceswhat-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.
在任何情况下什么那么曾经是一个对象的任何部分曾经“第一”或“第二”,或在对象中的任何命令。这在 ECMA-262 规范中有明确的概述。浏览器供应商有时会适应这种行为,这就是有时“它有效”的原因。
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.
所以使用一个数组。方括号表示一个数组,它不以字符串作为键(这是对象所做的)。更令人困惑的是,数组是对象,因此它们可以像对象一样运行——不要混淆它并认为对象是数组。
回答by Dexygen
myObject["123"] = { "C": 123, "D": 456 };
does notadd 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:
对于#2 和#3,Javascript 对象不保证按添加顺序返回属性;为此,您将不得不求助于数组,然后在将数据调整为不同数据结构的限制后,您可以获得第一个元素:
myArray.shift()
回答by James Montagne
You could use Object.keys()
in browsers which support it:
您可以Object.keys()
在支持它的浏览器中使用:
console.log("first remaining property is " + Object.keys(myObject)[0] );
Though I'm unsure if order is guaranteed with either approach.
虽然我不确定这两种方法是否能保证订单。