2016-07-06 100 views
0

我有一個像下面的JavaScript對象:JavaScript不能獲取對象的屬性值

var row = { 
       'fileAttribute' : { 
        '.\K\ar- #%i\.n/a': true, 
        'size': '2040', 
        'type' : 'pdf' 
        } 
    }; 

但是,當我試圖讓row.fileAttribute [價值「\ķ\ AR-#%中/年。」 ],我得到未定義,而不是真實的。

我知道這是我的屬性名稱包含特殊字符。 row.fileAttribute.hasOwnProperty('。\ K \ ar-#%i.n/a')返回false。我試圖通過_.values(row.fileAttribute ['。\ K \ ar-#%i.n/a'])提取這個屬性值,但是我得到了空數組。

我的控制檯: enter image description here

下面是這個問題的一個例子:

http://jsfiddle.net/fvu2pqzz/8/

我明白任何幫助,謝謝!

+0

您鏈接到的代碼是顯著不同於'row.fileAttributes [」。\ķ\ AR-#%的/一'])' – Quentin

+0

似乎工作:http://jsfiddle.net/fvu2pqzz/8/ – pawel

+0

錯別字錯誤。它應該是'fileAttribute'而不是's',shouldn'tit? –

回答

0

您需要使用bracket notation

console.info(row.entity.fileAttribute['.Kar- #%i.n/a']); 

var row = { 
 
      'entity' : { 
 
       'fileAttribute' : { 
 
        '.\K\ar- #%i\.n/a': true, 
 
        'size': '20', 
 
        'qty' : '50' 
 
       }, 
 
       'part2' : { 
 
        'name': 'Part 2', 
 
        'size': '15', 
 
        'qty' : '60' 
 
       } 
 
      } 
 
     }; 
 
     
 
console.info(row.entity.fileAttribute['.Kar- #%i.n/a']);

+0

shivgre,不幸的是不工作 –

+0

@AminPourhadi我剛剛測試它與您的對象和它返回true – shivgre

+0

@AminPourhadi你一定在做別的 – shivgre