2015-11-06 40 views
-1

傢伙我有在json format.It一直encoded數組看起來象下面這樣: -如何從jquery/javascript中統計一個數組的鍵名?

{"result":[{"pid":"24","uf":"hancy061","ot":"sanjit061","dn":"November 6,2015 at 02:18pm","view":"0","nu":"hancy061"},{"pid":"23","uf":"hari061","ot":"sanjit061","dn":"November 6,2015 at 01:09pm","view":"0","nu":"hari061"}]} 
現在

總之,我的問題是,我可以算的view總數從jQuery的/ JavaScript的我想要什麼?來計算view並將該號碼存儲在var。請注意,這可能不算什麼大事。有沒有什麼辦法可以從jquery/javascript中計算出view?幫助需要的人。

+0

你可以'JSON_decode'並遍歷它('的foreach decodedArray AS鍵=>值)'和'計數鍵的出現次數=='view'' – Burki

+0

你可以在回覆中發佈這對我和你都會有好處。 –

回答

2

您可以使用RegExp.match

var data = JSON.stringify({ 
 
    "result": [{ 
 
     "pid": "24", 
 
     "uf": "hancy061", 
 
     "ot": "sanjit061", 
 
     "dn": "November 6,2015 at 02:18pm", 
 
     "view": "0", 
 
     "nu": "hancy061" 
 
    }, { 
 
     "pid": "23", 
 
     "uf": "hari061", 
 
     "ot": "sanjit061", 
 
     "dn": "November 6,2015 at 01:09pm", 
 
     "view": "0", 
 
     "nu": "hari061" 
 
    }] 
 
}); 
 

 
var count = (data.match(/view/g) || []).length; 
 
console.log(count);

+1

@SOuřaanGřg試試這個http://jsfiddle.net/_alexander_/y17oqky0/ –

+1

@SOuřaanGřg我已經添加了一些說明http://jsfiddle.net/_alexander_/y17oqky0/2/ –

相關問題