对于循环中仅过滤对象
For...in loop filtering only objects
有没有办法过滤掉for。。。循环中只获取对象?
我正在编写一个函数,循环遍历嵌套对象以查找某些数据片段,然后将其保存到localStorage。
示例:
var equipped = {
data: [the rest of the properties of equipped go here],
tool: {
data: [the rest of the properties of tool go here],
axe: {
data: [the rest of the properties of axe go here],
iron: {...},
steel: {...}
}
}
}
刀具/斧头/金属特性都是动态生成的,每次都不同。金属属性的内部是我试图保存的数据。如果我试图访问数据,我通常只会循环遍历数组(使用knockoutjs进行绑定,只对数据数组进行foreach要容易得多),但我使用的是for。。。在字符串化之前,在我的localStorage对象中构建树的其余部分。
我如何阅读对象:
for (var type in equipped) {
if (check goes here) {
savedValue.equipped[type] = {};
for (var category in equipped[type]) {
etc etc...
}
}
}
我知道一切都是一种对象类型,所以我不能只对定义的对象执行instanceof
或typeof
来过滤它们。有没有其他简单的方法可以在if语句中完成它,或者我必须从构造函数中完成树的每一步,这样我才能instanceof RealObject
?
其中任何一个都应该做得很好:
function isObject(val) {
if (val === null) { return false;}
return (typeof val === 'object');
}
或
function isObject(obj) {
return obj === Object(obj);
}
或//这只适用于对象文字
function isObject(val) {
return (!!val) && (val.constructor === Object);
};
最后一个,给了我以下内容:
console.log(isObject()); // false
console.log(isObject([])); // false
console.log(isObject(new Date)); // false
console.log(isObject({})); // true
console.log(isObject(null)); // false
console.log(isObject(true)); // false
console.log(isObject(1)); // false
console.log(isObject('someValueString')); // false
所以,类似于:
for (var type in equipped) {
if (isObject(type)) {
savedValue.equipped[type] = {};
for (var category in equipped[type]) {
etc etc...
}
}
}
注意:你也可以尝试以下内容,但我没有使用过。所以你必须仔细查看你的用例。
Object.getPrototypeOf
以下是检查变量是否为对象的代码:
function isJsonObject( obj ) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || obj.toString() !== "[object Object]" || obj.nodeType || obj.setInterval ) {
return false;
}
// Not own constructor property must be Object
if ( obj.constructor
&& !obj.hasOwnProperty("constructor")
&& !obj.constructor.prototype.hasOwnProperty("isPrototypeOf")) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for ( key in obj ) {}
return key === undefined || obj.hasOwnProperty( key );
}
我以前用过一个旧的类型检测方法。
var classChecker = {}.toString;
classChecker.call({});
classChecker.call(function() {});
classChecker.call([]);
// etc...
// More immediately relevant use:
var savedValue = {
equipped: {}
};
var objectString = classChecker.call({});
for (var type in equipped) {
if (classChecker.call(equipped[type]) === objectString) {
savedValue.equipped[type] = {};
for (var category in equipped[type]) {
// ...
}
}
}
console.log(savedValue);
请参阅http://plnkr.co/edit/nKLQsOdcurrpUCg7cOoJ?p=preview对于工作样品。(打开控制台查看输出)
相关文章:
- 执行过滤对象数组的方法
- 如何按数组/对象值的倍数过滤对象数组
- 访问KendoUI中Datasource过滤对象的字段
- 通过 nodejs 过滤对象数组
- 如何过滤对象内部深度堆叠的数据(并在之后编辑删除它)
- 如何使用filter函数在javascript中过滤对象
- Javascript:按字符串数组过滤对象数组
- 按对象属性过滤对象的角度
- RXJS5 - 按每个对象包含的可观察性过滤对象数组
- 通过使用 AngularJS 遵循 OR 运算符仅过滤对象中的特定字段
- 在 lodash 中按键过滤对象
- Javascript 过滤对象
- Javascript - 如何按参数过滤对象数组
- 使用过滤器和下划线过滤对象数组
- 过滤对象数组时的无限$digest循环
- 使用Moment.js按“上个月”和“上周”单击时过滤对象数组
- 如何在 javascript 中过滤对象中具有特定后缀的键/值
- 按对象的属性过滤对象的 JavaScript 数组并删除重复项
- 对于循环中仅过滤对象
- 通过ng repeat内的关键帧过滤对象