对于循环中仅过滤对象

For...in loop filtering only objects

本文关键字:过滤 对象 循环 于循环      更新时间:2023-09-26

有没有办法过滤掉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...
        }
    }
}

我知道一切都是一种对象类型,所以我不能只对定义的对象执行instanceoftypeof来过滤它们。有没有其他简单的方法可以在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对于工作样品。(打开控制台查看输出)