我在JavaScript中创建了一个对象,由于某种原因,它没有保存对象中的第17和第20项。
你可以通过下面的代码看到我的意思,第17和第10代。添加后,第20项不会记录到控制台。
为什么会这样,我必须遗漏一些明显的东西。我正在创建对象,因为我放入的值将用作整个AngularJS应用程序中可用的常量。
var foo = {
001: { id: 001, name: "foo bar"},
002: { id: 002, name: "foo bar"},
003: { id: 003, name: "foo bar"},
004: { id: 004, name: "foo bar"},
005: { id: 005, name: "foo bar"},
006: { id: 006, name: "foo bar"},
007: { id: 007, name: "foo bar"},
008: { id: 008, name: "foo bar"},
009: { id: 009, name: "foo bar"},
010: { id: 010, name: "foo bar"},
011: { id: 011, name: "foo bar"},
012: { id: 012, name: "foo bar"},
013: { id: 013, name: "foo bar"},
014: { id: 014, name: "foo bar"},
015: { id: 015, name: "foo bar"},
016: { id: 016, name: "foo bar"},
017: { id: 017, name: "foo bar"},
018: { id: 018, name: "foo bar"},
019: { id: 019, name: "foo bar"},
020: { id: 020, name: "foo bar"}
}
console.log(foo);
答案 0 :(得分:5)
问题是Javascript中以0
开头的数字文字被视为八进制(即基数为8)。所以020 === 16
。如果数字以0
开头但包含数字9
或8
(即它是无效的八进制数),则将其视为正常的十进制数。
因此,当您执行017
和020
时,它会被视为15
和16
,因此该属性会被写入两次。
如果您想要数字键,则需要将它们全部放在同一个基础上,因此丢失前导0
。如果你想要字面意思020
,那么你需要创建键和值字符串:
"001": { id: "001", name: "foo bar"}
答案 1 :(得分:4)
那是因为这不是十进制数的有效格式。发生了什么事,你正在写octal numbers。基本上,它们是基数为8而不是10。
如果您希望它们位于基数10,请删除前导零。
var foo = {
1: { id: 1, name: "foo bar"},
2: { id: 2, name: "foo bar"},
3: { id: 3, name: "foo bar"},
4: { id: 4, name: "foo bar"},
5: { id: 5, name: "foo bar"},
6: { id: 6, name: "foo bar"},
7: { id: 7, name: "foo bar"},
8: { id: 8, name: "foo bar"},
9: { id: 9, name: "foo bar"},
10: { id: 10, name: "foo bar"},
11: { id: 11, name: "foo bar"},
12: { id: 12, name: "foo bar"},
13: { id: 13, name: "foo bar"},
14: { id: 14, name: "foo bar"},
15: { id: 15, name: "foo bar"},
16: { id: 16, name: "foo bar"},
17: { id: 17, name: "foo bar"},
18: { id: 18, name: "foo bar"},
19: { id: 19, name: "foo bar"},
20: { id: 20, name: "foo bar"}
}
console.log(foo);
如果想要前导零,则需要使用字符串。
var foo = {
"001": { id: "001", name: "foo bar"},
"002": { id: "002", name: "foo bar"},
"003": { id: "003", name: "foo bar"},
"004": { id: "004", name: "foo bar"},
"005": { id: "005", name: "foo bar"},
"006": { id: "006", name: "foo bar"},
"007": { id: "007", name: "foo bar"},
"008": { id: "008", name: "foo bar"},
"009": { id: "009", name: "foo bar"},
"010": { id: "010", name: "foo bar"},
"011": { id: "011", name: "foo bar"},
"012": { id: "012", name: "foo bar"},
"013": { id: "013", name: "foo bar"},
"014": { id: "014", name: "foo bar"},
"015": { id: "015", name: "foo bar"},
"016": { id: "016", name: "foo bar"},
"017": { id: "017", name: "foo bar"},
"018": { id: "018", name: "foo bar"},
"019": { id: "019", name: "foo bar"},
"020": { id: "020", name: "foo bar"}
}
console.log(foo);
答案 2 :(得分:1)
我认为它与从0开始的数字有关,在javascript中被解释为八进制。所以017 = 15,你只需覆盖15的记录。
如果您将键名作为显式字符串,则可以正常工作。 同样适用于内心' id'每条记录的关键。 这就是为什么我不使用以数字开头的对象键,如果每个键只是一个索引,则使用数组。
var foo = {
'001': { id: '001', name: "foo bar"},
'002': { id: '002', name: "foo bar"},
'003': { id: '003', name: "foo bar"},
'004': { id: '004', name: "foo bar"},
'005': { id: '005', name: "foo bar"},
'006': { id: '006', name: "foo bar"},
'007': { id: '007', name: "foo bar"},
'008': { id: '008', name: "foo bar"},
'009': { id: '009', name: "foo bar"},
'010': { id: '010', name: "foo bar"},
'011': { id: '011', name: "foo bar"},
'012': { id: '012', name: "foo bar"},
'013': { id: '013', name: "foo bar"},
'014': { id: '014', name: "foo bar"},
'015': { id: '015', name: "foo bar"},
'016': { id: '016', name: "foo bar"},
'017': { id: '017', name: "foo bar"},
'018': { id: '018', name: "foo bar"},
'019': { id: '019', name: "foo bar"},
'020': { id: '020', name: "foo bar"}
}
console.log(JSON.stringify(foo));
答案 3 :(得分:1)
因为前缀数字为0会告诉编译器(即浏览器)该数字是八进制且017 = 15
所有索引在转换为字符串时都要进行八进制到十进制转换(注意日志中的“”),有些将覆盖
其他数字(如019)不是有效的八进制值,所以它们的值保持十进制格式我认为