如何在javascript中计算对象

时间:2012-01-05 05:25:26

标签: 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,但我想要对象计数。

2 个答案:

答案 0 :(得分:8)

您可以使用length

obj.employees.length

这是JSFiddle:http://jsfiddle.net/wavXY/3/

我建议使用JSON.parse代替eval。请仔细阅读此链接以获取更多详细信息:http://www.json.org/js.html

答案 1 :(得分: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

....等

希望它有所帮助。