lundi 10 juin 2019

How to display array objects using a simple loop and if statements?

I have got 3 arrays and a couple of simple loops. I want to specify 3 conditions that would show a person from Warsaw at Web developer position with salary over 2000. The problem is that it shows two records instead of one.

I've tried writing conditions inside each loop but none of my combination did work.

var people = [
    {'name': 'Viola',       'salary': 2500, 'surname': 'Smith'},
    {'name': 'Boris',   'salary': 1300, 'surname': 'Popkovitch'},
    {'name': 'John',        'salary': 500,  'surname': 'Lynn'},
    {'name': 'Tom',         'salary': 3300, 'surname': 'Gates'},
    {'name': 'Levis',   'salary': 900, 'surname': 'Klark'},

];

var workplace = [
    {'city': 'New York',        'persons': ['Viola']},
    {'city': 'Manchester',  'persons': ['John', 'Boris']},
    {'city': 'Warsaw',          'persons': ['Tom', 'Levis']},
];

var job = [
        {'position': 'Head manager',    'workers': ['Boris']},
    {'position': 'Web developer', 'workers': ['Tom', 'Viola']},
    {'position': 'Principal',       'workers': ['Levis', 'John']}
];

var array = [];

for (var x = 0; x < people.length; x++) {
    for (var y = 0; y < workplace.length; y++) {
    for (var z = 0; z < job.length; z++) {
        if (workplace[y].city === 'Warsaw' && job[z].position === 'Web developer' && people[x].salary > 2000) {
        array.push(people[x]);
      }
    }
  }
};


console.log(array);

I expect the code to return only Tom object, not Tom and Viola. Any idea?

Aucun commentaire:

Enregistrer un commentaire