Show an element of a JSON with an ID

0

How can I show the iP that belongs to an "ip_id" within elements?.

data.json

{
"json": [{
    "list_ip": [{
        "1": "190.15.20.18",
        "2": "10.10.10.100",
        "3": "10.20.30.40"
    }],
    "cosas": [{
        "elementos": [{
                "ip_id": 1
            },
            {
                "ip_id": 3
            }
        ]
    }]
}]

}

javascript

    $.each( data.json, function(index_json, valores ) {

      $.each(valores.cosas.elementos, function(index_elementos, valores_elementos){
        /*Aqui se debería mostrar la IP que pertenece a ese ip_id*/
        /*por ejemplo = "190.15.20.18"*/
        console.log(valores_elementos.ip_id:1);
      });
    });

Result:

190.15.20.18
    
asked by Santy SC 20.04.2017 в 17:17
source

4 answers

0

I do not see the complication. If as you say, the data is always at the same level, just:

  • Get the list of ips.
  • Get the array of elements.
  • Perform a foreach to add to a new object, the ips found according to id_ip of each object within elementos .
  • const data = {
      "json": [
        {
          "list_ip": [
            {
              "1": "190.15.20.18",
              "2": "10.10.10.100",
              "3": "10.20.30.40"
            }
          ],
          "cosas": [
            {
              "elementos": [
                 {
                   "ip_id": 1
                 },
                 {
                   "ip_id": 3
                 }
              ]
            }
          ]
        }
      ]
    };
    
    
    function mapIPs() {
      const ips = data.json[0].list_ip[0];
      const els = data.json[0].cosas[0].elementos;
      const associations = {};
      
      els.forEach(el => {
        for(let [key, val] of Object.entries(el)) {
          if (ips[val]) {
            associations[val] = ips[val];
          }
        }
      });
      
      return associations;
    }
    
    console.info(mapIPs());
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        
    answered by 20.04.2017 / 18:35
    source
    0

    I leave this example working, since the structure of that JSON is not very useful to relate. In case that the ip_list and the elements were not in position 0 you must do another algorithm to find those variables and store them in the corresponding variables.

        var jsonEstilo1=[
        {
            "list_ip": [
                {
                    "1": "190.15.20.18",
                    "2": "10.10.10.100",
                    "3": "10.20.30.40"
                }
            ],
            "cosas": [
                {
                    "elementos": [
                        {
                            "ip_id": 1
                        },
                        {
                            "ip_id": 3
                        }
                    ]
                }
            ]
        }
    ];
    
        function searchlistIpEstilo1(id){
            //----- La lista de ip asumiento que siempre estará en la posicion 0
            var iplist=jsonEstilo1[0].list_ip[0];
            if(id in iplist){
                return iplist[id];
            }else{
                return null;
            }
        }
        //----- La lista de elmentos asumiento que siempre estará en la posicion 0
          var elementos=jsonEstilo1[0].cosas[0].elementos;
        for(var i=0;i<elementos.length;i++){
            var result=searchlistIpEstilo1(elementos[i].ip_id);
            if(result!=null){
                console.log(result);
            }
    }
    
        
    answered by 20.04.2017 в 18:06
    0

    So I solved it:

            var ips;    
            var ip_ids;
            $.each( data.json, function(index_json, valores ) {
              ips = valores.list_ip
              $.each(valores.cosas.elementos, function(index_elementos, valores_elementos){
                ip_ids = valores_elementos.ip_id;
                $.each(ips, function(index_ip, valor_ips){
                        ip_el = valor_ips[ip_ids];
                });
              console.log(ip_el);
              });
            });
    
        
    answered by 20.04.2017 в 20:24
    0

    Here is an example of how to extract the ip according to the value of ip_id :

    obj = {
     "json": [{
      "list_ip": [{
       "1": "190.15.20.18",
       "2": "10.10.10.100",
       "3": "10.20.30.40"
      }],
      "cosas": [{
       "elementos": [{
        "ip_id": 1
       },{
        "ip_id": 3
       }]
      }]
     }]
    }
    display(obj,"","ip_id");
    
    function display(xobj,sp,search) {
     for (n in xobj) {
      if (typeof xobj[n] == 'object') {
       display(xobj[n],n+".");
      }else{
       if (n=="ip_id")
        html.innerHTML+="<p>"+sp+n+"="+xobj[n]+" ("+obj.json[0].list_ip[0][xobj[n]]+")";
      }
     }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="html"></div>
        
    answered by 20.04.2017 в 18:53