Evan Lalo
Evan Lalo

Reputation: 1273

Loop through dynamic object - Jquery

I know this can be done fairly easily I just can't figure it out. I have a dynamic object, generated by SPARQL queries. It returns JSON like this:

data = {
    "p": { "type": "uri" , "value": "http://www.company.com/PDE/SpecGuide#MatrixColumn" } ,
    "o": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "value": "1" } ,
    "obj": { "type": "uri" , "value": "http://www.company.com/PDE/SpecGuide#airtouch" } ,
    "lbl": { "type": "literal" , "value": "Airtouch" }
  } ,
  {
    "p": { "type": "uri" , "value": "http://www.company.com/PDE/SpecGuide#MatrixRow" } ,
    "o": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "value": "1" } ,
    "obj": { "type": "literal" , "value": "Smooth_Paint" } ,
    "hdr": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#boolean" , "value": "true" }
  } 

The only thing I am concerned with are the keys and the value['value']so I need to filter out the rest. Ultimately, I am looking for this:

data = [
{1: {p: "http://www.company.com/PDE/SpecGuide#MatrixColumn",
     o: "1", 
     obj : "Smooth_Paint"} ,
{2 : {p: "http://www.company.com/PDE/SpecGuide#MatrixColumn",
     o: "1", 
     obj : "other_values"}
     otherKey: "more_values}
]

This is driving me crazy because I know this isn't especially difficult but I cannot figure it out. I have tried variations of

$.each(data, function(key,valueObj){
   data.key = {key: valueObj.value};
   list.push(data)
});

but I end up with variations of this:

{1:{key: "http://www.company.com/PDE/SpecGuide#MatrixColumn"}}
{2: {key: "1"}}
{3:{key: "http://www.company.com/PDE/SpecGuide#airtouch"}}
{4:{key: "Airtouch"}}

Upvotes: 0

Views: 54

Answers (1)

Hikarunomemory
Hikarunomemory

Reputation: 4305

The first variable in $.each() callback is always an index.

You can use for...in to get the properties attached to the object itself with hasOwnProperty(). See more

var data = [{
    "p": { "type": "uri" , "value": "http://www.company.com/PDE/SpecGuide#MatrixColumn" } ,
    "o": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "value": "1" } ,
    "obj": { "type": "uri" , "value": "http://www.company.com/PDE/SpecGuide#airtouch" } ,
    "lbl": { "type": "literal" , "value": "Airtouch" }
  } ,
  {
    "p": { "type": "uri" , "value": "http://www.company.com/PDE/SpecGuide#MatrixRow" } ,
    "o": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "value": "1" } ,
    "obj": { "type": "literal" , "value": "Smooth_Paint" } ,
    "hdr": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#boolean" , "value": "true" }
  }]
  var list = [];
  $.each(data, function(index, obj){
    var newData = {}
    for(key in obj){
        if(obj.hasOwnProperty(key))
        {
            newData[key] = obj[key]['value']
        }
    }
    var data = {}
    data[list.length+1] = newData
    list.push(data)
  })
  console.log(list)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 3

Related Questions