如何在javascript中计算对象数
这是我的代码,我想数一数其中的对象数 使用eval函数,我可以得到元素,但我不知道如何计算其中的所有对象。谁能帮帮我吗如何在javascript中计算对象数,javascript,Javascript,这是我的代码,我想数一数其中的对象数 使用eval函数,我可以得到元素,但我不知道如何计算其中的所有对象。谁能帮帮我吗 var txt = '{ "employees": [{ "a": "wkn", "d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process
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/
我能够获取元素,但我需要对象计数。您可以使用
长度
:
obj.employees.length
以下是JSFIDLE:
我建议使用
JSON.parse
而不是eval
。请浏览此链接了解更多详细信息:作为一名JS noob,看着你的员工数组,我自己也很想知道如何遍历它。我做了一些实验,也许这不是最有效的方法,但它帮助我理解了如何遍历和计算这样的东西
这是小提琴:
……等等
希望有帮助。不要使用eval,请使用JSON.parse
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