2016-12-22 92 views
1

我已經在JavaScript中創建了一個對象,並且由於某種原因,它沒有將對象中的第17項和第20項保存起來。從我剛剛創建的Object中缺少JavaScript項目

您可以看到我的意思是下面的代碼,第17個&第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);

回答

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

5

的問題是在Javascript這個數字文字,與0開始作爲一個八進制(即基座8)處理。所以020 === 16。如果一個數字以0開頭,但包含數字98(即它是一個無效的八進制數),則它被視爲正常的十進制數。

所以當你做017020,它被視爲1516,使物業被寫入到兩次。

如果你想要數字鍵,你需要使它們全部在同一個基地,所以失去領先0。如果你想從字面上020,那麼你需要做的鍵和值的字符串:

"001": { id: "001", name: "foo bar"} 
+1

是不是有關於在嚴格模式或ES6中更改八進制的東西? IIRC有一些東西,但我找不到它。我可能是錯的。 –

+2

@FelixKling是的,八進制('0o020')和一個新的二進制格式('0b0110')有一個新的格式。請參閱[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Numeric_literals)。 – lonesomeday

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)); 
1

因爲爲0加上數字前綴告訴編譯器(即,瀏覽器)的數量是一個八進制和017 = 15

所以所有的指標,如有一個八進制到十進制的轉換而轉換成字符串(注意「在日誌)和」部分將覆蓋

其他數字(如019)是無效的八進制值,所以他們的價值保留在十進制形式,我認爲

相關問題