-1

I have a task for school to make a web application or api endpoint with json data. I need to display all that data and to display their friends, and friends of friends within persons object. Apparently I stopped here cause all my values within array in console are not defined (http://prntscr.com/i9i477), can someone help me with this, thanks !

class Osoba {
  constructor(id, firstName, surname, age, gender, friends) {
    this._id = id;
    this._firstName = firstName;
    this._surname = surname;
    this._age = age;
    this._gender = gender;
    this._friends = friends;
  }

  get id() {
    return this._id;
  }
  set id(id) {
    this._id = id;
  }
  get firstName() {
    return this._firstName;
  }
  set firstName(firstName) {
    this._firstName = firstName;
  }
  get surname() {
    return this._surname;
  }
  set surname(surname) {
    this._surname = surname;
  }
  get age() {
    return this._age;
  }
  set age(age) {
    this._age = age;
  }

  get gender() {
    return this._gender;
  }
  set gender(gender) {
    this._gender = gender;
  }

  get friends() {
    return this._friends;
  }
  set friends(friends) {
    this._friends = friends;
  }

}

osobe = [];

$(function() {
  $.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master / data.json ', function(data)  {
    var json = jQuery.parseJSON(JSON.stringify(data));

    for (person in json) {

      var _id = person['id'];
      var _firstName = person['firstName'];
      var _surname = person['surname'];
      var _age = person['age'];
      var _gender = person['gender'];
      var _friends = person['friends'];

      x = new Osoba(id = _id, firstName = _firstName, surname = _surname, 
        age = _age,gender = _gender, friends = _friends);
      osobe.push(x);
    }
  })
});

console.log(osobe);
  • 2
    You spelt constructor wrong, and there is a missing quote after the getJSON url. – James Feb 02 '18 at 22:26
  • i fixed it , it was spelling mistake but still i m stucked at this problem – Stefan Sipic Feb 03 '18 at 09:34
  • run seperately, first constructor and then this function – Saikat Chakrabortty Feb 03 '18 at 09:42
  • I voted for reopen, or at least to change the duplicate, because the main problem (the one that is described) is not because of the asnyc `async`, but because of the `for...in` – t.niese Feb 03 '18 at 09:54
  • [What is the difference between ( for… in ) and ( for… of ) in javascript?](https://stackoverflow.com/questions/29285897/what-is-the-difference-between-for-in-and-for-of-in-javascript) – t.niese Feb 03 '18 at 10:15

2 Answers2

1

The for...in loop iterats over the enumerable properties of an object of an object and not over the values of it (and it should not be used for arrays).

So for for (person in json) { the person is the index of 0 to n of the array.

Instead of the for...in loop you have to use the for...of loop.

Beside that the console.log of the final result has to be inside of the ajax callback, and the url contained multiple spaces. And those assignements in the constructor don't make any sense at all.

Correcting this will show the correct output:

class Osoba {
  constructor(id, firstName, surname, age, gender, friends) {
    this._id = id;
    this._firstName = firstName;
    this._surname = surname;
    this._age = age;
    this._gender = gender;
    this._friends = friends;
  }

  get id() {
    return this._id;
  }
  set id(id) {
    this._id = id;
  }
  get firstName() {
    return this._firstName;
  }
  set firstName(firstName) {
    this._firstName = firstName;
  }
  get surname() {
    return this._surname;
  }
  set surname(surname) {
    this._surname = surname;
  }
  get age() {
    return this._age;
  }
  set age(age) {
    this._age = age;
  }

  get gender() {
    return this._gender;
  }
  set gender(gender) {
    this._gender = gender;
  }

  get friends() {
    return this._friends;
  }
  set friends(friends) {
    this._friends = friends;
  }

}

osobe = [];

$(function() {
  $.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master/data.json', function(data) {
    var json = jQuery.parseJSON(JSON.stringify(data));

    for (person of json) {

      var _id = person['id'];
      var _firstName = person['firstName'];
      var _surname = person['surname'];
      var _age = person['age'];
      var _gender = person['gender'];
      var _friends = person['friends'];

      x = new Osoba(_id, _firstName, _surname, _age, _gender, _friends);
      osobe.push(x);
    }
    console.log(osobe);
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
t.niese
  • 39,256
  • 9
  • 74
  • 101
0

you need to call console.log after request end :

$(function() {
  $.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master / data.json ', function(data)  {
    var json = jQuery.parseJSON(JSON.stringify(data));

    for (person in json) {

      var _id = person['id'];
      var _firstName = person['firstName'];
      var _surname = person['surname'];
      var _age = person['age'];
      var _gender = person['gender'];
      var _friends = person['friends'];

      x = new
      Osoba(id = _id, firstName = _firstName, surname = _surname, age = _age,
            gender = _gender, friends = _friends);
      osobe.push(x);
    }

    console.log(osobe);
  })
});
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28