在JavaScript对象中使用For和 [英] Use of For each and found in javascript Objects
本文介绍了在JavaScript对象中使用For和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的对象.
var attr = {node:1; value:"2"
}
我知道每个原型都可以用于数组吗?同样可以用于对象数组?如果是,是否有任何方法可以检查对象数组中是否存在值?
Hi,
I have an object as shown below.
var attr={node:1;value:"2"
}
I know for each prototype can be used for array? the same can used for object array? If so is there any way to check whether a value exist in the object array?
推荐答案
首先,语法有误.
var attr = {node:1 ; value:"2"}
使用,
我认为forEach仅适用于数组.
First there is a mistake in syntax.
var attr={node:1;value:"2"}
use ,
I think forEach is available only for Arrays.
{node:1,value:"2"}
是一个对象.要使其成为数组,只需用"[]"包装
is an object. To make it an array, just wrap it with "[]"
[{node:1,value:"2"}]
选中此选项,让我知道...下面是使用Firefox测试的有效示例.
Check this and let me know... below is a working example tested with Firefox.
var obj = [{name:"John",age:25}];
obj.forEach(function(person){
alert(person.name+" is "+person.age+" years old");
});
这篇关于在JavaScript对象中使用For和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文