Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 4查找JSON数据_Json_Angular - Fatal编程技术网

Angular 4查找JSON数据

Angular 4查找JSON数据,json,angular,Json,Angular,我有这个JSON:(this.url) 我有这项服务: findUsers(id: number) : boolean { return this.http.get(this.url) .map((res: Response) => res.json()); .someOperator(to assert the id exist)... } 如果找到用户,我想返回true。 是否有像filter这样的运算符可以为我进行断言?尝试使用Array.some()

我有这个
JSON
:(this.url)

我有这项服务:

 findUsers(id: number) : boolean {
    return this.http.get(this.url)
    .map((res: Response) => res.json());
    .someOperator(to assert the id exist)...
  }
如果找到用户,我想返回
true

是否有像
filter
这样的运算符可以为我进行断言?

尝试使用
Array.some()
方法:

findUsers(id: number) : boolean {
  return this.http
             .get(this.url)
             .map((res: Response) => {
               let result = res.json();
               return result.user.some(user => parseInt(user.id) === id)
             })
}
在线模拟

const数据={
“用户”:[
{
“id”:“1”,
“名称”:“根”,
“密码”:“根”
},
{
“id”:“2”,
“名称”:“客户端测试”,
“密码”:“123456”
}
]
}
//这将模拟res.json()正在处理的http get解析
常数httpGet=Rx.可观察到的(数据);
const findUsers=id=>
httpGet.map(data=>data.user.some(user=>parseInt(user.id)==id))
;
订阅(result=>console.log(result));
订阅(result=>console.log(result))

您想查看结果是否为空吗?是的,这可以解决问题假设您的服务将返回一个空数组,您只需运行
result.length==0
查看它是否为空。如果我这样做:login(user:user){this.userService.getUsers().subscribe(users=>this.usersArray=users,error=>console.log(“error”+error));for(让usersArray的x){…}知道json内容在usersArray上,我尝试迭代,但控制台说:无法读取未定义的属性Length…属性'some'在类型'Observable'上不存在。你是对的,我在中没有看到它。你也可以尝试使用find和isEmpty运算符的组合。将更新我的回答是的,这应该可以工作,但不是。w当我使用.do(x=>console.log(x)时,它返回的是未定义的。它只返回带有以下内容的内容:.do(x=>console.log(x[0].id)…很好。它可以工作,但没有“.user”。只是“result.some”。顺便说一句,返回需要是可观察的
findUsers(id: number) : boolean {
  return this.http
             .get(this.url)
             .map((res: Response) => {
               let result = res.json();
               return result.user.some(user => parseInt(user.id) === id)
             })
}