如何检查地址(类别city:mtm)并使用我的.ts文件中的typescript从json获取特定的用户详细信息?

时间:2017-07-08 02:59:11

标签: angular typescript ionic-framework

我已经在我的json下面给了。

  

我需要检查地址类别类型城市。但我能得到   输出。我在下面给出的是我的json对象。

[
  {
    "username": "ramu",
    "user_id": "222",
    "profileImage": "ramu.jpg",
    "status": "15",
    "address": [
      {
        "type": "permanent",
        "city": "mtm",
        "pin": "654654",
        "personal": [
          {
            "age": "26",
            "color": "white"
          }
        ]
      },
      {
        "type": "residential",
        "city": "chennai",
        "pin": "76755624",
        "personal": [
          {
            "age": "26",
            "color": "green"
          }
        ]
      }
    ]
  },
  {
    "username": "raju",
    "user_id": "222",
    "profileImage": "ramu.jpg",
    "status": "15",
    "address": [
      {
        "type": "permanent",
        "city": "mtm",
        "pin": "521001",
        "personal": [
          {
            "age": "26",
            "color": "red"
          }
        ]
      },
      {
        "type": "residential",
        "city": "chennai",
        "pin": "600024",
        "personal": [
          {
            "age": "26",
            "color": "blue"
          }
        ]
      }
    ]
  }
]

任何机构都会帮我查一下(地址类别city:mtm)并仅获取特定的用户详细信息?

2 个答案:

答案 0 :(得分:0)

即使你按照city =“mtm”过滤你的json,它也会返回当前json的所有值。假设你有一个不同的json,你可以这样做。

<强>样本

var myArray = [
  {
    "username": "ramu",
    "user_id": "222",
    "profileImage": "ramu.jpg",
    "status": "15",
    "address": [
      {
        "type": "permanent",
        "city": "mtm",
        "pin": "654654",
        "personal": [
          {
            "age": "26",
            "color": "white"
          }
        ]
      },
      {
        "type": "residential",
        "city": "chennai",
        "pin": "76755624",
        "personal": [
          {
            "age": "26",
            "color": "green"
          }
        ]
      }
    ]
  },
  {
    "username": "raju",
    "user_id": "222",
    "profileImage": "ramu.jpg",
    "status": "15",
    "address": [
      {
        "type": "permanent",
        "city": "mtm",
        "pin": "521001",
        "personal": [
          {
            "age": "26",
            "color": "red"
          }
        ]
      },
      {
        "type": "residential",
        "city": "chennai",
        "pin": "600024",
        "personal": [
          {
            "age": "26",
            "color": "blue"
          }
        ]
      }
    ]
  }
];

let filteredArray = myArray
  .filter((element) => 
    element.address.some((address) => address.city === 'mtm'));
    
console.log(filteredArray);

答案 1 :(得分:0)

class AddressLookUp{
    lookup:{ [index:string]: any};
    userArray:Array<Object>;
    constructor(users:Array<Object>){
        this.userArray = users;
        this.lookup={};
        this._buildLookUp()
    }
     private _buildLookUp():void{
        this.userArray.forEach((currentU:Object,_indexU:number)=>{
            (<any>currentU).address.forEach((currentA:Object,_indexA:number)=>{
                if((<any>currentA).city in this.lookup){
                    this.lookup[(<any>currentA).city].push(_indexU)
                }else{
                    this.lookup[(<any>currentA).city]=new Array(_indexU)
                }
            })
        })
    }
    findByAddress(address:string):Array<Object>{
        return  this.userArray.filter((c,i)=>{
            return i in this.lookup[address]
        }) 
    }
}

然后使用它

let lookup = new AddressLookUp(userArray)
console.log(JSON.stringify(lookup.findByAddress("mtm")))