如何使用 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]]
我们可以使用 Number
构造函数来过滤数组 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]
?如果我们在 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]]
字符串
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,其他方法 */)
返回匹配对象的过滤结果,甚至是输入数组中对象的属性名称或值.
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.
我们开始,很天真,检查数组中的项目是否有一个 constructor
的 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
prototype
和 constructor
是 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
的数组:{}
对象;即使对象没有定义的原型或构造函数;可选地将JSON
字符串"{"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)
模式,其中 this
作为属性名称或对象的值;或使用使用 filter.bind
、 .call
或 .apply
或其他方法从输入数组中过滤对象的方法 - 不使用 full
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")
?
在搜索类似问题并找到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.
注意,问题也可以通过 destructuring
或其他 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.
问题:
如何在不使用匿名函数的情况下过滤传递给
Array.prototype.filter()
的数组中带有或不带有Object
原型或构造函数的对象 <代码>回调代码>模式?
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 ?
如何在传递给 Array.prototype.filter()
的数组中过滤特定对象,方法是将属性名称或值传递给匹配对象而不使用匿名函数 callback
> 模式?
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.
让我们从以下开始:
var types = ['1', 2, true, null, undefined, [], {}, new Date()];
并运行以下命令:
types.map((e) => typeof e);
// ["string", "number", "boolean", "object", "undefined", "object", "object", "object"]
你认为 null
是一个 Object
吗?我不这么认为.您是否认为 Array
是 Object
,因为 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
以及任何其他具有任何 prototype
的 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屋!