如何在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
等
希望能有所帮助。
相关文章:
- 面向对象JavaScript中的私有函数
- 对象 Javascript 中的标签无效 - 想要添加事件列表器
- 将字符串转换为对象 javascript/jquery
- 正在检查对象javascript中是否存在嵌套属性
- 时间-日期对象JavaScript getUTCMilliseconds
- 仅在对象(javascript)中解析值
- 使用对象(JavaScript或jQuery)填充选择下拉列表
- 引用另一个对象javascript中的对象
- 如何删除列表中的对象?Javascript nodejs和下划线
- 无法从日期对象javascript获取日期和月份
- 如何在if语句中使用对象-Javascript
- 对象javascript错误
- 对象Javascript的少数实例
- 位置对象Javascript
- 将字符串传递到对象javascript中
- 获取对象Javascript或jQuery的最后一个值
- 鼠标接近对象Javascript
- 这个mixins代码是书中的错误吗;面向对象JavaScript的原理”;
- 使用闭包编译器编写更好的面向对象JavaScript完整示例代码
- 访问对象javascript数组中的对象属性值