Hide JSON element using javascript

1

I have the following result in JSON:

 {
  "name": "test",
  "count": 5,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Wed Jun 24 2015 16:25:51 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "property1": "",
        "property2": "DIA DA TERRA",
        "index": 1,
        "url": "http://site.com.br"
      },

I need to show only the property2 element, how to hide the remaining elements (property1, index, url) using javascript?

    
asked by anonymous 24.06.2015 / 19:03

3 answers

1

and only use as an example object:

var json = {
  "name": "test",
  "count": 5,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Wed Jun 24 2015 16:25:51 GMT+0000 (UTC)",
  "results": {
    "collection1": [
        
      {
        "property1": "",
        "property2": "DIA DA TERRA",
        "index": 1,
        "url": "http://site.com.br"
      },
      {
        "property1": "",
        "property2": "DIA DA TERRA 2",
        "index": 1,
        "url": "http://site.com.br"
      }
    ]
    
  }
}

alert(json.results.collection1[0].property2);
alert(json.results.collection1[1].property2);

only access it by object. JSFIDDLE

    
24.06.2015 / 19:11
0

GustavoCave , if your JSON comes as a parameter to an ajax function (ex: success , done , etc), literally ignore keys propery1 , index and url .

Example

$.ajax( "recuperaJSON.php" )
.done(function(resultado) {

    // EXIBIR SOMENTE PROPRIEDADE "property2"
    // para cada resultado
    $.each(data['results'], function(key, value) 
    {
        $.each(data['results'][key], function(key_result, value_result)
        {
            // considere haver um elemento HTML com id "idResultado", por exemplo 
            $('#idResultado').append(value_result['property2'] + "<br>");
        });    
    });

});

Demo

See your JSON saved in a variable called json and then iterating between results by displaying only propery2 :

link

    
24.06.2015 / 20:00
0

Well, I got it using the following:

function transform(data) { 
    data.results.collection1.map(function(item) {  
       delete item.index;  
       return item;  
    }); 
    return data; 
};
    
26.06.2015 / 12:33