Armin
Armin

Reputation: 89

JSON JavaScript Accessing data

I'm reading chapter 5 of Eloquent JavaScript: A Modern Introduction to Programming Book. I found this JSON online and my goal is to exercise what I learned from the book. I have some background on Java however because JavaScript is a loosely typed language declarations and parsing are a bit confusing. The code in coma does compile but what I have inside loop doesn't. Though it's obviously wrong, to my logic that line of code shouldn't make a different inside of loop or outside loop. Is the logic wrong or the syntax?

The Error i'm getting is:

Cannot read property 'died' of undefined

var DATA = "[\n  " + [
'{"name": "Carolus Haverbeke", "sex": "m", "born": 1832, "died": 1905, 
"father": "Carel Haverbeke", "mother": "Maria van Brussel"}',
'{"name": "Emma de Milliano", "sex": "f", "born": 1876, "died": 1956, 
"father": "Petrus de Milliano", "mother": "Sophia van Damme"}',
'{"name": "Maria de Rycke", "sex": "f", "born": 1683, "died": 1724, 
"father": "Frederik de Rycke", "mother": "Laurentia van Vlaenderen"}',
'{"name": "Jan van Brussel", "sex": "m", "born": 1714, "died": 1748, 
"father": "Jacobus van Brussel", "mother": "Joanna van Rooten"}',
'{"name": "Philibert Haverbeke", "sex": "m", "born": 1907, "died": 1997, 
"father": "Emile Haverbeke", "mother": "Emma de Milliano"}',
'{"name": "Jan Frans van Brussel", "sex": "m", "born": 1761, "died": 1833, 
"father": "Jacobus Bernardus van Brussel", "mother":null}',
'{"name": "Pauwels van Haverbeke", "sex": "m", "born": 1535, "died": 1582, 
"father": "N. van Haverbeke", "mother":null}',
'{"name": "Clara Aernoudts", "sex": "f", "born": 1918, "died": 2012, 
"father": "Henry Aernoudts", "mother": "Sidonie Coene"}',
'{"name": "Emile Haverbeke", "sex": "m", "born": 1877, "died": 1968, 
"father": "Carolus Haverbeke", "mother": "Maria Sturm"}',
'{"name": "Lieven de Causmaecker", "sex": "m", "born": 1696, "died": 1724, 
"father": "Carel de Causmaecker", "mother": "Joanna Claes"}',
'{"name": "Pieter Haverbeke", "sex": "m", "born": 1602, "died": 1642, 
"father": "Lieven van Haverbeke", "mother":null}',
'{"name": "Jacobus Bernardus van Brussel", "sex": "m", "born": 1736, "died": 
1809, "father": "Jan van Brussel", "mother": "Elisabeth Haverbeke"}'
].join(",\n  ") + "\n]";

if (typeof module != "undefined" && module.exports)
    module.exports = DATA;

function Search() {
    // console.log(result[1].born);
    var result = JSON.parse(DATA);
    var a1,a2;

    for(var i=0; i<result.length;i++)
    {
        a2= +result[i].died;
        a1 = +result[i].born; 

        if(a1 >= 1800 && ((a2-a1) <=40))
        {   
            console.log(result[i].name);    
        } 
    } 
}

Upvotes: 0

Views: 73

Answers (2)

pirs
pirs

Reputation: 2463

1 - It's not "dead" but "died", you just misreaded..

2 - Try to filter

console.log(result[i]);
if(result[i].died && result[i].born){
   a2=  +result[i].died;
   a1 = +result[i].born;
}

3 - your loop goes too far, change it for(var i=0; i < result.length - 1;i++)

Upvotes: 1

fluoresce
fluoresce

Reputation: 452

You have an off by one error

for(var i=0; i<=result.length;i++)
{
   ...
} 

Your array has 12 items, but the loop runs 13 times.

for(var i=0; i<=result.length - 1 ;i++)

or

for(var i=0; i < result.length ;i++)

Upvotes: 0

Related Questions