How do I display array objects using a simple loop and ‘if’ statements?


Ask A Question





Login To post a question

I have got three arrays and a couple of simple loops. I want to specify three conditions that would show a person from Warsaw at a Web developer position with a 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 the Tom object, not Tom and Viola. Any idea?

For more info please click herehttps://stackoverflow.com/q/56533487



Have a question or need a custom quote?
info@smartinfocare.com