0

When loop adds objects into array, i m getting double values within it

enter image description here

and when i want to call some object from array page its telling me its not defined (undefined), is there any possible way to make my object to increment per loop so it will be osoba, osoba1, osoba2 , can someone help me how to fix that,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;
  }

}

var 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'];

      var x = new Osoba(id, firstName, surname, age, gender, friends);
      osobe.push(x);
    }
    console.log(osobe);
  })
  document.write(osobe[0]);
});
  • 1
    Why are you doing `jQuery.parseJSON(JSON.stringify(data))`? Just use `data` itself. – Barmar Feb 03 '18 at 10:40
  • 1
    Why do you need separate variables? Just use `osobe[0]`, `osobe[1]`, etc. – Barmar Feb 03 '18 at 10:44
  • 1
    You haven't shown how you're trying to use the array, but my guess is that your problem is answered [here](https://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron?newsletter=1&nlcode=97716%7c4ba7) – Barmar Feb 03 '18 at 10:45
  • I dont need it but i m curious is that even possible , and i can use osobe[0] it shows me undefined on the page – Stefan Sipic Feb 03 '18 at 10:45
  • `window['osobe' + i] = x;` will do it, if you increment `i`. – Barmar Feb 03 '18 at 10:46
  • But it will still be undefined if you try to access the variable before the asynchronous function completes. – Barmar Feb 03 '18 at 10:47
  • Put `document.write(osobe[0])` inside the `$.getJSON` callback function. – Barmar Feb 03 '18 at 10:48
  • what if i want to call a whole object not just his ID or a Name – Stefan Sipic Feb 03 '18 at 10:53
  • If you want to see the name, use `document.write(osobe[0].surname)` – Barmar Feb 03 '18 at 10:55
  • i want to see a whole object with all values – Stefan Sipic Feb 03 '18 at 10:56
  • `document.write(JSON.stringify(osobe[0]))`. Or use `console.log` instead of `document.write`. – Barmar Feb 03 '18 at 10:57
  • okay thank you, one more question , how can i make that friends array within the object make an output string ( friends name ) and not an ID – Stefan Sipic Feb 03 '18 at 10:59
  • You'll need to loop through the `person.friends` array, and find the `Osoba` object with that ID. It would probably be best if you made `osobe` an object whose keys are the IDs, instead of an array, then you could use `osobe[person.friends[i]]` to find that friend object. – Barmar Feb 03 '18 at 11:01
  • You'll probably have to do that after you create all the `Osoba` objects, because a friend might not appear until after the one the has it in its friends list. – Barmar Feb 03 '18 at 11:02
  • how can i do that, and i will need two loops i guess because i will need to find friends of certain object , and friends of friends of a certain object – Stefan Sipic Feb 03 '18 at 11:04
  • Are you expecting me to write your application for you? – Barmar Feb 03 '18 at 11:06
  • nope just a hints i m still a newbie and i m reading everything and trying to figure out things from the codes – Stefan Sipic Feb 03 '18 at 11:07
  • As I said, use an object instead of an array. After you've created all the Osobe objects, loop through them, and then loop through the `friends` arrays, doing `this._friends[i] = osobe[this._friends[i]];`. This will replace the IDs with the corresponding objects. – Barmar Feb 03 '18 at 11:12
  • but i got all osobe object within the array, i just made them and keep them in that array so i can manipulate with them – Stefan Sipic Feb 03 '18 at 11:45
  • I'm trying to tell you that an object is better, because then you can find other objects by their IDs quickly. – Barmar Feb 03 '18 at 11:47
  • but how can i put an object inside the object – Stefan Sipic Feb 03 '18 at 11:50
  • Exactly as I showed in my comment above. You can store objects anywhere. – Barmar Feb 03 '18 at 11:55
  • The thing is that i dont want to catch their friends i want when i make document.write(JSON.stringify(osobe[0])) output of friends array to be a string(their name ) instead of int (their id ) – Stefan Sipic Feb 03 '18 at 12:00
  • Then change what I wrote to `this._friends[i] = osobe[this._friends[i]]._surname;` – Barmar Feb 03 '18 at 12:06
  • The point is that you still have to find the object with that ID, then get the name from it. – Barmar Feb 03 '18 at 12:06
  • can i make a function within the class and to make that function to compare id with osoba id and if its same to add their name and surname instead of id? – Stefan Sipic Feb 03 '18 at 12:15
  • You can do whatever you like. I'm just telling you what I consider the best design. Searching an array to look up each ID is bad design. – Barmar Feb 03 '18 at 12:20
  • but man i cant make an empty object and put objects inside of it trough the for of loop and i dont know how to do that – Stefan Sipic Feb 03 '18 at 12:25
  • Yes you can. Initialize it as `osobe = {}`. Then do `osobe[x.id] = x;` – Barmar Feb 03 '18 at 12:27
  • It sounds like you need to learn the language better. I can't help you with that. – Barmar Feb 03 '18 at 12:28
  • osobe[x.id] = x; should be going before var x = new Osoba(id,firstName,surname,age,gender,friends); osobe.push(x); – Stefan Sipic Feb 03 '18 at 12:29
  • How can it go before that? You can't use `x` until after you set it. – Barmar Feb 03 '18 at 12:30
  • This is not filling my object with these objects from for of loop – Stefan Sipic Feb 03 '18 at 12:35
  • I give up. When you've written the code, post a new question and we'll try to help you. We can't do this in comments.] – Barmar Feb 03 '18 at 12:41
  • well to be honest this is my first task in javascript, all i did before was java – Stefan Sipic Feb 03 '18 at 12:49
  • This stuff is common to most modern programming languages. A Javascript object is similar to a Java Map. – Barmar Feb 03 '18 at 12:50
  • Yeah thats why i made all of this with really little knowledge of javascript and i m stucked here now i guess i need double for loop to loop trough my array and friends array and somehow to change ID of friends array for firstname and surname of objects in my array – Stefan Sipic Feb 03 '18 at 13:07

0 Answers0