如何使用Array.prototype.filter过滤对象? [英] How to filter Object using Array.prototype.filter?
问题描述
给定
var arr = [1,2,true,4,{"abc":123},6,7,{"def":456},9,[10]]
我们可以过滤数字项在 arr
中使用编号
构造函数
we can filter number items within array arr
using Number
constructor
var res = arr.filter(Number); // [1, 2, true, 4, 6, 7, 9, Array[1]]
在结果数组中有 true
和 [10]
如果我们将 false
替换为 true
在 arr
are true
and [10]
expected in resulting array ? If we substitute false
for true
at arr
var arr = [1,2,false,4,{"abc":123},6,7,{"def":456},9,[10]]
var res = arr.filter(Number) // [1, 2, 4, 6, 7, 9, Array[1]]
使用 Array.isArray
var res = arr.filter(Array.isArray) // [Array[1]]
String
var res = arr.filter(String) // [1, 2, true, 4, Object, 6, 7, Object, 9, Array[1]]
如果我们要过滤 arr
中的项目,那些对象是索引 4
, 7
,我们尝试
If we want to filter items within arr
that are object, at indexes 4
, 7
and we try
var res = arr.filter(Object) // [1, 2, true, 4, Object, 6, 7, Object, 9, Array[1]]
尽管我们只想调用 arr.filter(Object)
,我们可以传递一个函数调用;尝试 Object
的不同属性,以便我们最终可以找到一个可以用作函数或构造函数的属性或方法作为模式传递给 arr .filter(/ * method,constructor,other approach * /)
返回与对象匹配的过滤结果,甚至返回输入数组内对象的属性名称或值。
Although we would prefer to simply call arr.filter(Object)
, we could pass a function call; trying different properties of Object
so that we can eventually find a property or method that we could use as a function or constructor to pass to as the pattern arr.filter(/* method, constructor, other approach */)
to return the filtered results matching the object, or even property name or value of the object within the input array.
我们通过检查数组中的项是否有构造函数
具有 name
等于Object
We start, innocently enough, by checking if the item in the array has a constructor
having name
equal to "Object"
var res = arr.filter(function(prop) {
return prop.constructor.name === "Object"
}) // [Object, Object]
虽然我们添加一个对象到 arr
;例如;
though when we add an object to arr
; e.g.;
var c = Object.create(null); arr.push(c);
var res = arr.filter(function(prop) {
return prop.constructor.name === "Object"
}) // `Uncaught TypeError: Cannot read property 'name' of undefined`
as c
原型和构造函数
是 undefined
。虽然我们确定这不会返回预期的结果
as c
prototype
and constructor
are undefined
. Although we are certain that this will not return expected results
var n = arr.filter(Object.hasOwnProperty, "abc"); // [1, 2]
至少没有返回错误;让我们继续
at least an error was not returned; let us continue
var n = arr.filter(function(prop, val) {
return prop.hasOwnProperty(this.valueOf())
}, "abc"); // [Object abc: 123__proto__: Object]
返回预期结果;虽然我们试图使用
the expected results are returned; though we are trying to use
var n = arr.filter(/* function reference */, this /* optional parameters passed */)
至
-
过滤一个数组
Object
:{}
objects;即使对象没有定义的原型或构造函数;可以将JSON
string{abc:123}
转换为对象;虽然我们还没有达到这个目标,但是,
filter an array for
Object
:{}
objects; even if the object does not have a defined prototype or constructor; optionally convertingJSON
string"{"abc":123}"
to object; though we have not reached this far, yet;
将属性名称传递给 .filter(callback,this)
pattern,其中此
用作属性名称或对象的值;或者使用 filter.bind
, .call
或 .apply
或其他方法从输入数组过滤对象 - 不使用完整的
pass a property name to .filter(callback, this)
pattern where this
serves as property name, or value of object; or utilize an approach using filter.bind
, .call
or .apply
or other method to filter an object from the input array - without using full
.filter(function(prop,value){} )
模式。我们如何强制 Object.hasOwnProperty()
调用类似于
pattern. How can we coerce the Object.hasOwnProperty()
call into a pattern similar to
.filter(Object.hasOwnProperty,abc)
?
提到 .call
, .bind
和 。在寻找类似的问题后,应用
并找到 JS原型方法上的Array.prototype.filter 。虽然不清楚如何实现如上所述过滤具有特定属性的对象和对象所描述的方法。
Mentioning .call
, .bind
and .apply
after searching for a similar Question and finding JS Array.prototype.filter on prototype method . Though not certain how to implement approaches described in filtering both objects and objects having specific properties as described above.
注意,问题也可以通过解构
或其他 es-6
, es-7
方法,提供可比较或,与 .filter()
相比,更加严格的结果。也就是说,使用 .filter()
没有
Note, Question can also be resolved by a destructuring
, or other es-6
, es-7
approach, providing comparable or, even stricter results, when compared to .filter()
. That is, use .filter()
without
function(prop, value) {
}
返回物体;这是 Object
, {}
;和通过属性过滤的对象;对象按属性值过滤。
pattern. Returning objects; that is Object
, {}
; and objects filtered by property ; objects filtered by property value.
问题:
-
如何使用或不使用
Object
在传递给Array.prototype.filter的数组中的原型或构造函数过滤对象()
不使用匿名函数callback
pattern?
How to filter objects with or without
Object
prototype or constructor within in an array passed toArray.prototype.filter()
without using an anonymous functioncallback
pattern ?
通过将属性名或值传递给匹配对象而不使用匿名函数 callback pattern?
How to filter specific objects within an array passed to Array.prototype.filter()
by passing property name or value to match object without using anonymous function callback
pattern ?
推荐答案
没有真正的方法安全地做,而不必创建自己的功能。此外,这是非常复杂的,因为 Object
的定义太广泛了。
There is no real way to do it safely without creating your own function. Additionally it is very complicated because the definition of Object
is too broad.
让我们从以下开始: p>
Let's start with the following:
var types = ['1', 2, true, null, undefined, [], {}, new Date()];
并运行以下命令:
types.map((e) => typeof e);
// ["string", "number", "boolean", "object", "undefined", "object", "object", "object"]
您是否认为 null
为 Object
?我不这么认为您认为 Object
的 Array
,因为 Array
是 Object
的实例?我不太清楚。
Do you think of null
of as an Object
? I don't think so. Do you think of an Array
as of an Object
, because the Array
is an instance of Object
? I am not sure as well.
您可以尝试以下方式:
types.map(Object.isExtensible);
// [false, false, false, false, false, true, true, true]
这不包括结果中的 null
,但仍然存在数组。 Date
Object
在这里以及任何其他 Object
任何原型
,例如 new Boolean()
也将是一个 Object
。此外,该对象可以被冻结,并且这不会作为 Object
在这里返回。
This excludes the null
from the result but still the array is present here. The Date
Object
is here as well as any other Object
with any prototype
, e.g. new Boolean()
will also be an Object
. Additionally the object could be frozen and this won't be returned as an Object
here as well.
所以这里的两个例子都成功地证明了 Object
的定义太广泛了,它不能以一种有用的方式真正处理。
So the both examples here successfully demonstrate that the definition of Object
is too broad and it cannot be really handled in a useful way.
这篇关于如何使用Array.prototype.filter过滤对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!