Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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/0/iphone/43.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
Json 角度2(类型脚本)-RxJS可观察-groupBy_Json_Angular_Rxjs - Fatal编程技术网

Json 角度2(类型脚本)-RxJS可观察-groupBy

Json 角度2(类型脚本)-RxJS可观察-groupBy,json,angular,rxjs,Json,Angular,Rxjs,json数据 { "name": "Sam", "age":25, "schools": [ { "country":"USA", "state":"IN", "city":"Fishers", "name":"school 1" }, { "country":"USA", "state":"IN", "city":"Indianapolis",

json数据

{
 "name": "Sam",
 "age":25,
 "schools":
 [
     {
       "country":"USA",
       "state":"IN",
       "city":"Fishers",
       "name":"school 1"
     },
     {
       "country":"USA",
       "state":"IN",
       "city":"Indianapolis",
       "name":"school 2"
     },
     {
       "country":"USA",
       "state":"CO",
       "city":"Denver",
       "name":"school 3"
     },
     {
        "country":"Canada",
       "state":"Ontario",
       "city":"Toronto",
       "name":"school 4"
     }
   ]
}
我需要在学校数组上按“国家”分组,然后按“州”分组,然后按“城市”分组

我的打字脚本界面

interface Person{
name:string;
age:number;
schools: School[];
}

interface School{
country:string;
states: State[];
}

interface State{
state:string;
pairs: Pair[];
}

interface Pair{
city:string;
name:string;
}
我需要编写一个服务方法来将json数据转换到我的模型中,以便学校按国家分组,国家按州分组,州按城市和名称分组

我的模型结构正确吗?如果是,我如何根据需要分组

我的服务方式应该是这样的

getPerson():Observable<Person>
{
      return this.http.get(url to get json data).groupBy(XYZ..).....
}
getPerson():可观察
{
返回此.http.get(获取json数据的url).groupBy(XYZ…..)。。。。。
}

这不是帮助,而是请求。。。不管怎样,这里是:

groupBy(array: any[], property: string) {
    return array.reduce((prev, next) => {
        // If the group doesn't exist, create it
        if(!prev[property]) { prev[property] = [next]; }
        // Else, we push it in
        else { prev[property].push(next); }
        // Mandatory return
        return prev;
    }, {});
}
现在您有了一个分组对象。您可以使用它,也可以将其转换为数组

可能重复的