类型“对象"上不存在属性“过滤器" [英] Property 'filter' does not exist on type 'Object'

查看:222
本文介绍了类型“对象"上不存在属性“过滤器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将代码更新为HttpClient之后,我无法使用filter()处理响应.我一直在获取类型"对象"上不存在的属性过滤器":

After updating my code to HttpClient I can't get filter() to work with the response. I keep getting Property 'filter' does not exist on type 'Object':

TS :

liqOnChange(selection): void  {
        this.http.get(this.json_liq).subscribe(res => {
            this.arr = res.filter(
                res => res.id == selection.target.value);
        });
}

推荐答案

filter是一种数组方法,在您的情况下res不能用于对象.

filter is an array method which cannot be used for objects in your case res.

首先检查res是否为数组.

if(Array.isArray(res)){
   this.arr = res.filter(
                res => res.id == selection.target.value);
}

或将类型设置为res作为(res: Array<any>)

如果您的api返回一个对象,则在大多数情况下应该是这样,您不能在此处使用filter方法,而应该将其用于res对象内部的数组.

If your api is returning an object, in most cases it should be, you can't use filter method here, you should be using it for an array inside res object.

这篇关于类型“对象"上不存在属性“过滤器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆