2012-01-05 55 views
0

這裏是我的代碼,我想計算這個對象的數量。如何在javascript中計算對象

使用eval函數我能夠得到的元素,但不知道如何計算在這個總的對象。任何人都可以幫我嗎?

var txt = '{ 
    "employees": [{ 
     "a": "wkn", 
     "d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services", 
     "n": "", 
     "u": "http://wipro.com/", 
     "t": ["outsourcing", "offshore", "india"], 
     "dt": "2009-06-26T10:26:02Z" 
    }, { 
     "a": "shaktimaan", 
     "d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services", 
     "n": "Wipro Technologies is the No 1 provider of integrated business, technology and process solutions on a global delivery platform.", 
     "u": "http://wipro.com/", 
     "t": ["Indian", "IT", "Services", "Companies"], 
     "dt": "2011-09-16T17:31:25Z" 
    }, { 
     "a": "tonemcd", 
     "d": "Offshore Outsourcing | IT Services", 
     "n": "", 
     "u": "http://wipro.com/", 
     "t": ["outsourcing", "IT"], 
     "dt": "2007-11-04T03:53:18Z" 
    }] 
}'; //added \n for readability 

var obj = eval ("(" + txt + ")"); 



document.getElementById("fname").innerHTML=obj.employees[1].a 
document.getElementById("lname").innerHTML=obj.employees[1].u 

這是我得到這個響應:

First Name: shaktimaan 
Last Name: http://wipro.com/ 

我能夠獲取elemtns,但我想的對象計數。

+1

不要使用eval,請使用JSON.parse – 2012-01-05 05:29:14

回答

1

作爲一個JS noob,看着你的員工數組我自己想知道它是如何被遍歷的。我嘗試了一些,也許這不是最有效的方式,但它幫助我理解如何遍歷和計算這樣的事情。

這裏的小提琴:http://jsfiddle.net/bSMQn/

var txt = '{"employees":[{"a": "wkn", "d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services", "n": "", "u": "http://wipro.com/", "t": ["outsourcing", "offshore", "india"], "dt": "2009-06-26T10:26:02Z"}, {"a": "shaktimaan", "d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services", "n": "Wipro Technologies is the No 1 provider of integrated business, technology and process solutions on a global delivery platform.", "u": "http://wipro.com/", "t": ["Indian", "IT", "Services", "Companies"], "dt": "2011-09-16T17:31:25Z"}, {"a": "tonemcd", "d": "Offshore Outsourcing | IT Services", "n": "", "u": "http://wipro.com/", "t": ["outsourcing", "IT"], "dt": "2007-11-04T03:53:18Z"}]}'; 

var obj = eval ("(" + txt + ")"); 
var i =0; 
for (;i<obj.employees.length;i++) 
{ 
    document.writeln("Employee " + i+":<br/><br/>"); 
    var j = 0; 
    for(v in obj.employees[i]) 
    { 
     j++; 
     document.writeln(v + " => " + obj.employees[i][v] +"<br/>"); 
    } 
    document.writeln("<b>Count:" + j +"</b>"); 
    document.writeln("<hr/><br/>"); 

} 

輸出

Employee 0: 

a => wkn 
d => Wipro Technologies – IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services 
n => 
u => http://wipro.com/ 
t => outsourcing,offshore,india 
dt => 2009-06-26T10:26:02Z 
Count:6 

Employee 1: 

a => shaktimaan 
d => Wipro Technologies – IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services 
n => Wipro Technologies is the No 1 provider of integrated business, technology and process solutions on a global delivery platform. 
u => http://wipro.com/ 
t => Indian,IT,Services,Companies 
dt => 2011-09-16T17:31:25Z 
Count:6 

....等

希望它能幫助。

相關問題