如何在javascript中计数对象

how to count object in javascript

本文关键字:对象 javascript      更新时间:2023-09-26

这是我的代码,我想计算对象的数量。

使用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/

我可以获取元素,但我想要对象计数

您可以使用length:

obj.employees.length

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

我建议使用JSON.parse而不是eval。请点击此链接:http://www.json.org/js.html

作为一个JS新手,看着你的员工数组,我自己想知道它是如何被遍历的。我做了一些实验,也许这不是最有效的方法,但它帮助我理解了如何遍历和计数这样的东西。

小提琴在这里: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

希望能有所帮助。