0

Having this variable:

var pico = {
        "dc:title": 0,
        "dc:identifier": 0,
        "dc:subject": 0,
        "dc:type": 0,
        "pico:preview": 0,
        "dc:isReferencedBy": 0,
        "dcterms:license": 0,
        "pico:licenseMetadata": 0,

    };  

and this json response:

{ 
   '$':{ 
      'xmlns:pico':'http://purl.org/pico/1.0/',
      'xmlns:dc':'http://purl.org/dc/elements/1.1/',
      'xmlns:dcterms':'http://purl.org/dc/terms/',
      'xmlns:xsi':'http://www.w3.org/2001/XMLSchema-instance',
      'xsi:schemaLocation':'http://purl.org/pico/1.0/    http://purl.org/pico/1.0/pico.xsd'
   },
   'dc:description':{ 
      _:'L’antica porta urbica, incorporata negli edifici circostanti, fu ridotta a un solo fornice. Sul lato interno, Madonna col Bambino e santi, affresco del sec. XIV.',
      '$':{ 
         'xml:lang':'it'
      }
   },
   'dc:identifier':'57926',
   'dc:subject':{ 
      _:'http://culturaitalia.it/pico/thesaurus/4.0#mura_fortificazioni',
      '$':{ 
         'xsi:type':'pico:Thesaurus'
      }
   },
   'dc:title':{ 
      _:'Arco delle due Porte, Siena',
      '$':{ 
         'xml:lang':'it'
      }
   },
   'dc:type':{ 
      _:'PhysicalObject',
      '$':{ 
         'xsi:type':'dcterms:DCMIType'
      }
   },
   'dcterms:isReferencedBy':{ 
      _:'http://www.touringclub.com/monumento/toscana/siena/arco-delle-due-porte.aspx',
      '$':{ 
         'xsi:type':'dcterms:URI'
      }
   },
   'dcterms:spatial':{ 
      _:'PlaceName=via Stalloreggi ; city=Siena ; province=SI',
      '$':{ 
         'xsi:type':'pico:PostalAddress'
      }
   }
}

If a property from the variable exist also in the JSON response I would assign to the property the value 1, if not exist the value 0.

Then I would divide the obtained sum by the expected value 8 in order to have the ratio of the filled fields.

I made this:

for (var key in item.metadata["pico:record"]) {
            pico[key] = pico[key] || 1;
            pico[key] != pico[key] || 0;
        }

...

let somma = 0;
        for (var property in pico) {
            console.log(property);
            if (pico.hasOwnProperty(property)) {
                somma += pico[property];
            }
        }
        console.log(somma / 8);

However, as result I'm having 1 instead of 0.72. This because the script I made counts the properties instead of the values.

Then I approached the problem according to this issue. Again, I'm having 8 as sum.

Suggestions?

3
  • Where is pico:record in the JSON response? Commented Oct 8, 2019 at 21:58
  • 1
    What are you expecting pico[key] != pico[key] || 0; to do? != is the not-equals comparison operator, but you're not doing anything with the result. Commented Oct 8, 2019 at 21:59
  • pico:record is the root element, before $ Commented Oct 8, 2019 at 22:02

1 Answer 1

1

You should be looping over the properties in pico, not the JSON response. And your logical operators are just totally wrong.

var somma = 0;
for (var key in pico) {
    var found = item.metadata["pico:record"].hasOwnProperty(key) ? 1: 0;
    pico[key] = found;
    somma += found;
}
console.log(somma/8);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.