Is it possible to loop through the properties in a JavaScript object? For instance, I have a JavaScript object defined as this:
是否可以遍历JavaScript对象中的属性?例如,我有一个JavaScript对象定义如下:
myObject.options = {
property1: 'value 1',
property2: 'value 2'
};
Properties will get dynamically added to this object. Is there a way for me to just loop through and do a check if a property exists? If so, how?
属性将动态添加到此对象。有没有办法让我循环并检查属性是否存在?如果是这样,怎么样?
5 个解决方案
#1
23
Yes you can and lodash is not needed... i.e.
是的你可以和lodash不需要...即
for (var key in myObject.options) {
// check also if property is not inherited from prototype
if (myObject.options.hasOwnProperty(key)) {
var value = myObject.options[key];
}
}
Edit: the accepted answer (_.forOwn()) should be https://*.com/a/21311045/528262
编辑:接受的答案(_.forOwn())应为https://*.com/a/21311045/528262
#2
499
Use _.forOwn()
.
使用_.forOwn()。
_.forOwn(obj, function(value, key) { } );
https://lodash.com/docs#forOwn
https://lodash.com/docs#forOwn
Note that forOwn
checks hasOwnProperty
, as you usually need to do when looping over an object's properties. forIn
does not do this check.
请注意,forOwn检查hasOwnProperty,因为在循环对象的属性时通常需要这样做。 forIn不做这个检查。
#3
11
For your stated desire to "check if a property exists" you can directly use Lo-Dash's has
.
对于您所说的“检查房产是否存在”的愿望,您可以直接使用Lo-Dash的。
var exists = _.has(myObject, propertyNameToCheck);
#4
0
It would be helpful to understand why you need to do this with lodash. If you just want to check if a key exists in an object, you don't need lodash.
了解为什么需要使用lodash执行此操作会很有帮助。如果您只想检查对象中是否存在密钥,则不需要lodash。
myObject.options.hasOwnProperty('property');
If your looking to see if a value exists, you can use _.invert
如果您想查看值是否存在,可以使用_.invert
_.invert(myObject.options)[value]
#5
0
In ES6, it is also possible to iterate over the values of an object using the for..of
loop. This doesn't work right out of the box for JavaScript objects, however, as you must define an @@iterator property on the object. This works as follows:
在ES6中,还可以使用for..of循环迭代对象的值。但是,这不适用于JavaScript对象的开箱即用,因为您必须在对象上定义@@ iterator属性。其工作原理如下:
- The
for..of
loop asks the "object to be iterated over" (let's call it obj1 for an iterator object. The loop iterates over obj1 by successively calling the next() method on the provided iterator object and using the returned value as the value for each iteration of the loop. - for..of循环询问“要迭代的对象”(让我们称它为迭代器对象的obj1。循环通过在提供的迭代器对象上连续调用next()方法并使用返回的值作为迭代来迭代obj1。循环的每次迭代的值。
- The iterator object is obtained by invoking the function defined in the @@iterator property, or Symbol.iterator property, of obj1. This is the function you must define yourself, and it should return an iterator object
- 迭代器对象是通过调用obj1的@@ iterator属性或Symbol.iterator属性中定义的函数获得的。这是您必须自己定义的函数,它应该返回一个迭代器对象
Here is an example:
这是一个例子:
const obj1 = {
a: 5,
b: "hello",
[Symbol.iterator]: function() {
const thisObj = this;
let index = 0;
return {
next() {
let keys = Object.keys(thisObj);
return {
value: thisObj[keys[index++]],
done: (index > keys.length)
};
}
};
}
};
Now we can use the for..of
loop:
现在我们可以使用for..of循环:
for (val of obj1) {
console.log(val);
} // 5 hello
#1
23
Yes you can and lodash is not needed... i.e.
是的你可以和lodash不需要...即
for (var key in myObject.options) {
// check also if property is not inherited from prototype
if (myObject.options.hasOwnProperty(key)) {
var value = myObject.options[key];
}
}
Edit: the accepted answer (_.forOwn()) should be https://*.com/a/21311045/528262
编辑:接受的答案(_.forOwn())应为https://*.com/a/21311045/528262
#2
499
Use _.forOwn()
.
使用_.forOwn()。
_.forOwn(obj, function(value, key) { } );
https://lodash.com/docs#forOwn
https://lodash.com/docs#forOwn
Note that forOwn
checks hasOwnProperty
, as you usually need to do when looping over an object's properties. forIn
does not do this check.
请注意,forOwn检查hasOwnProperty,因为在循环对象的属性时通常需要这样做。 forIn不做这个检查。
#3
11
For your stated desire to "check if a property exists" you can directly use Lo-Dash's has
.
对于您所说的“检查房产是否存在”的愿望,您可以直接使用Lo-Dash的。
var exists = _.has(myObject, propertyNameToCheck);
#4
0
It would be helpful to understand why you need to do this with lodash. If you just want to check if a key exists in an object, you don't need lodash.
了解为什么需要使用lodash执行此操作会很有帮助。如果您只想检查对象中是否存在密钥,则不需要lodash。
myObject.options.hasOwnProperty('property');
If your looking to see if a value exists, you can use _.invert
如果您想查看值是否存在,可以使用_.invert
_.invert(myObject.options)[value]
#5
0
In ES6, it is also possible to iterate over the values of an object using the for..of
loop. This doesn't work right out of the box for JavaScript objects, however, as you must define an @@iterator property on the object. This works as follows:
在ES6中,还可以使用for..of循环迭代对象的值。但是,这不适用于JavaScript对象的开箱即用,因为您必须在对象上定义@@ iterator属性。其工作原理如下:
- The
for..of
loop asks the "object to be iterated over" (let's call it obj1 for an iterator object. The loop iterates over obj1 by successively calling the next() method on the provided iterator object and using the returned value as the value for each iteration of the loop. - for..of循环询问“要迭代的对象”(让我们称它为迭代器对象的obj1。循环通过在提供的迭代器对象上连续调用next()方法并使用返回的值作为迭代来迭代obj1。循环的每次迭代的值。
- The iterator object is obtained by invoking the function defined in the @@iterator property, or Symbol.iterator property, of obj1. This is the function you must define yourself, and it should return an iterator object
- 迭代器对象是通过调用obj1的@@ iterator属性或Symbol.iterator属性中定义的函数获得的。这是您必须自己定义的函数,它应该返回一个迭代器对象
Here is an example:
这是一个例子:
const obj1 = {
a: 5,
b: "hello",
[Symbol.iterator]: function() {
const thisObj = this;
let index = 0;
return {
next() {
let keys = Object.keys(thisObj);
return {
value: thisObj[keys[index++]],
done: (index > keys.length)
};
}
};
}
};
Now we can use the for..of
loop:
现在我们可以使用for..of循环:
for (val of obj1) {
console.log(val);
} // 5 hello