如何使用Array.prototype.filter过滤对象? [英] How to filter Object using Array.prototype.filter?
问题描述
由于
变种改编= [1,2,真实,4,{ABC:123},6,7-,{DEF:456},9,[10]]
我们可以阵列改编
使用内筛选一些项目编号
构造
VAR解析度= arr.filter(数字); // [1,2,真实的,4,6,7,9,阵列[1]]
是真正
和 [10]
预期结果数组?如果我们替换假
为真正
在改编
变种改编= [1,2,假,4,{ABC:123},6,7-,{DEF:456},9,[10]]
变种解析度= arr.filter(数字)// [1,2,4,6,7,9,阵列[1]]
使用
Array.isArray
VAR解析度= arr.filter(Array.isArray)// [数组[1]
字符串
VAR解析度= arr.filter(字符串)// [1,2,真实,4,对象,6,7,对象,9,数组[1]
如果我们要过滤在项目改编
是对象,在指数 4
, 7
,我们尝试
VAR解析度= arr.filter(对象)// [1,2,真实,4,对象,6,7,对象,9,数组[1]
虽然我们很想preFER只需拨打 arr.filter(对象)
,我们可以通过一个函数调用;试图对象
的不同属性,使我们最终可以发现,我们可以用一个函数或构造函数传递给为图案 ARR的属性或方法.filter(/ *方法,构造函数,另一种方法* /)
返回过滤后的结果相匹配的对象,甚至是属性名称或输入数组内的对象的值。
我们开始,傻傻的是,通过检查数组中的项目都有一个构造
有名称
等于对象
VAR解析度= arr.filter(功能(道具){
返回prop.constructor.name ===对象
})// [对象,对象]
不过,当我们添加一个对象改编
;例如;
变种C =的Object.create(NULL); arr.push(C); VAR解析度= arr.filter(功能(道具){
返回prop.constructor.name ===对象
})//`遗漏的类型错误:无法读取属性'名'undefined`的
为 C
原型
和构造
是未定义
。虽然我们确信这将不会返回预期的效果。
变种N = arr.filter(Object.hasOwnProperty,ABC); // [1,2]
至少没有返回错误;让我们继续
变种N = arr.filter(功能(道具,VAL){
返回prop.hasOwnProperty(this.valueOf())
},ABC); // [对象ABC:123__proto__:对象]
预期的结果返回;虽然我们试图用
变种N = arr.filter(/ *函数引用* /这/ *可选参数传递* /)
到
-
筛选
对象
数组:{}
对象;即使对象没有一个确定的原型或构造;任选将JSON
字符串{ABC:123}
对象;虽然我们还没有达到这一步,但; -
传递属性名到
.filter(回调,这一点)
模式,其中这个
作为财产命名,或对象的值;或使用利用的方法filter.bind
,.CALL
或。适用
或其它方法来过滤输入数组的对象 - 无需使用完全.filter(功能(道具,价值){})
模式。我们怎样才能要挟
Object.hasOwnProperty()
调用到类似的模式。.filter(Object.hasOwnProperty,ABC)
一提 .CALL
, .bind
和。适用
寻找一个类似的问题和原型法求 JS Array.prototype.filter后。尽管不是一定如何实现在滤波对象和具有特定性质如上所述。对象描述的方法
请注意,问题也可以通过解构
或其他 ES-6
,<$ C $解决C> ES-7 的办法,相比于 .filter时提供媲美或更严格的结果,()
。也就是说,使用 .filter()
无
功能(道具,价值){ }
模式。返回对象;这是对象
, {}
;和对象按属性过滤;通过对象的属性值过滤。
问题:
-
如何带或不带过滤对象
对象
传递给数组Array.prototype.filter在内部原型或构造函数()
不使用匿名函数回调
模式? -
如何传递给
数组Array.prototype.filter()
内通过传递属性名称或值以匹配对象而不使用匿名函数过滤特定的对象回调
模式?
还有就是要做到安全使用,不会创建自己的功能没有真正的方法。此外,它是非常复杂的,因为对象
的定义过于宽泛。
让我们先从以下内容:
VAR类型= ['1',2,真实,空,不确定的,[] {},新的日期()];
和运行以下命令:
types.map((E)=&GT;的typeof E);
// [串,号码,布尔,对象,未定义,对象,对象,对象]
你觉得空作为一个
的?我不认为如此。你认为一个对象
阵列
为对象的
,因为阵列
的是的对象的实例
?我不知道为好。
什么你可以尝试如下:
types.map(Object.isExtensible);
// [假的,假的,假的,假的,假的,真实的,真实的,真正]
这排除了空
从结果,但仍然数组是present在这里。在日期
对象
在这里以及任何其他对象
与任何原型
,例如: 新布尔()
也将是一个对象
。 这里此外,该对象可以被冻结,这将不会返回一个对象也是如此。
所以这两个例子在这里成功地证明的定义对象
过于宽泛,无法以有用的方式得到真正处理。
Given
var arr = [1,2,true,4,{"abc":123},6,7,{"def":456},9,[10]]
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]]
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]]
using 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]]
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]]
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.
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]
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
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 */)
to
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;pass a property name to
.filter(callback, this)
pattern wherethis
serves as property name, or value of object; or utilize an approach usingfilter.bind
,.call
or.apply
or other method to filter an object from the input array - without using full.filter(function(prop, value) {})
pattern. How can we coerce the
Object.hasOwnProperty()
call into a pattern similar to.filter(Object.hasOwnProperty, "abc")
?
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.
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) {
}
pattern. Returning objects; that is Object
, {}
; and objects filtered by property ; objects filtered by property value.
Questions:
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 ?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 functioncallback
pattern ?
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.
Let's start with the following:
var types = ['1', 2, true, null, undefined, [], {}, new Date()];
and run the following:
types.map((e) => typeof e);
// ["string", "number", "boolean", "object", "undefined", "object", "object", "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.
What you can try is the following:
types.map(Object.isExtensible);
// [false, false, false, false, false, true, true, true]
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.
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屋!