Home Loop through dynamic object - Jquery
Reply: 0

Loop through dynamic object - Jquery

user1844
1#
user1844 Published in April 25, 2018, 12:36 am

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"}}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316274 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO