0

I have a Web application that consumes data from another Web service and they are in JSON format. I subscribed the data as results[] and I can access each of field by getting each of index in html, for example, {{results?.ABC.D[0].Name[0]}} and it returns Susan. (Please refer to the data sample below).

What I want to do is to search in results to find out Name and return G. Basically, I would like to acquire G's when they are in a same array D with Name.

{
    "ABC": {
        "D": [
            {
                "Name": [ "Susan" ],
                "F": [ "School_0" ],
                "G": [ "14" ]
            },
            {
                "Name": [ "Lydia" ],
                "F": [ "School_1" ],
                "G": [ "284" ]
            }
        ]
    }
}
James
  • 157
  • 2
  • 3
  • 9

3 Answers3

4

I think this is what you need:

Suppose that the data sample we have is the following:

 let dataSample =
      {
        "ABC": {
          "D": [
            {
              "Name": [
                "Susan",
                "Sophia"
              ],
              "F": [
                "School_0"
              ],
              "G": [
                "14"
              ]
            },
            {
              "Name": [
                "Lydia"
              ],

              "F": [
                "School_1"
              ],
              "G": [
                "284"
              ]
            }
          ]
        }
      }

if you want to iterate through all Name[ ], then you should also find the index of the name you want inside of the "Name" array and retrieve it, and then find the index of the "D" that contains that retrieved name. Like this:

    let names = ["Sophia", "Susan", "Lydia"];

    names.forEach(function(name) {
    this.findNameG(name)
    })

    function findNameG(nameToFind: String) {        
    let index = dataSample.ABC.D.findIndex(d => d.Name[d.Name.findIndex(name => name === nameToFind)]);
    console.log(dataSample.ABC.D[index].G);
}

So you need to find the index of the name first, and the return the value of G at that index.

Please consider that this is case sensitive, if you want to make it other wise you should use toLowerCase() or toUpperCase() methods for both source and target.

Hope this helps.

R. AbuBaker
  • 216
  • 2
  • 5
  • I would like to iterate through all Name[ ] – James Oct 23 '17 at 14:05
  • It works now, but how should I do this for multiple Names? So I have specific given Names and need to search for them and display respective Gs – James Oct 23 '17 at 18:57
  • So you place the above code in a function, and then loop for the specific given names and execute the function for each name individually. – R. AbuBaker Oct 23 '17 at 18:59
  • Declare the function the way it fits your environment, I am just writing a syntax :) Take a look here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function – R. AbuBaker Oct 23 '17 at 19:27
  • got it, but would it go through and retrieve all IDs for Names I defined? You have the === "Susan" there – James Oct 23 '17 at 19:27
  • Sorry forgot to change that, check now – R. AbuBaker Oct 23 '17 at 19:29
  • Hi R.AbuBarker, Thanks for the help.. last thing. Why am I getting property 'findLastValue' does not exist on type void? res is where I get JSON object from service. Please refer to updated post. – James Oct 23 '17 at 19:39
  • Your function is inside of ngOnInit, please move it outside and the call it like this: this.findLastValue(name); – R. AbuBaker Oct 23 '17 at 20:18
  • Also don't forget to fix your "names" array declaration. from this: let names = ["Suan, Mike"]; to this: let names = ["Suan", "Mike"]; – R. AbuBaker Oct 23 '17 at 20:20
0

Did you mean you want to return G , in condition you have Name[0] Value ? In this case :

 {{results?.ABC.D[0].Name[0] ? results?.ABC.D[0].G : '' }} 
AsmaG
  • 487
  • 4
  • 17
0

You can try this

jsonData = {
"ABC":{
   "D":[
      {
         "Name":[
            "Susan"
         ],
         "F":[
            "School_0"
         ],
         "G":[
            "14"
         ]
      },
      {
         "Name":[
            "Lydia"
         ],

         "F":[
            "School_1"
         ],
         "G":[
            "284"
         ]
      }
   ]
}
}

You can check like this, but i am sure there must be a simple solution

for (var key1 in this.jsonData) {
  if (this.jsonData.hasOwnProperty(key1)) {
      for (var key2 in this.jsonData[key1]){
        if (this.jsonData[key1].hasOwnProperty(key2)){
          if(key2 == "D"){
            for (var key3 in this.jsonData[key1][key2]){
              if (this.jsonData[key1][key2].hasOwnProperty(key3)){
                if(this.jsonData[key1][key2][key3]["Name"][0] == "Susan"){
                  console.log(this.jsonData[key1][key2][key3]["G"][0])
                }
              }
            }
          }
        }
      }
  }
}
Hareesh
  • 6,770
  • 4
  • 33
  • 60