Javascript Compare ERROR

Asked

Viewed 27 times

1

I’m trying to pass a down to validate a list I need but I’m not getting it to work and I don’t understand why yet.

I have the following code:

$('#aprovados').click( function(){
  $.getJSON($('#listeditappjobs').attr("data-list-app-my-jobs-url"),
    function(data){
      if (data){
        var apps = data.data
        console.log(apps[0])
        var infojob = data.datas
        console.log(infojob)
        var aprovados = []
        for(i = 0; i < apps.length; i++){
         if (infojob.distance >= apps[i].distance  &&
             infojob.xp <= apps[i].years_xp &&
             infojob.salary_min <= apps[i].pre_salary  &&
             infojob.salary_max >= apps[i].pre_salary)
            {aprovados.push(apps[i]); }
               };
        console.log(aprovados)
      }else{alert("LISTA vazia")
    }},
);
});

What I got back is:

Object { distance: 1, escolaridade: "TecnologoCursando", user: "[email protected]", pre_salary: "1500.12", years_xp: 3 }

Object { distance: 2, xp: 3, salary_max: "1500.01", salary_min: "200.00" }
Array []

What I’m doing wrong?

1 answer

1


The problem is that you are comparing numbers as strings:

infojob.salary_min <= apps[i].pre_salary
       |                     |
     "200.00"            "1500.12"

infojob.salary_max >= apps[i].pre_salary
       |                     |
     "1500.01"            "1500.12"

Use parseFloat() to convert them into numbers:

if (infojob.distance >= apps[i].distance  &&
    infojob.xp <= apps[i].years_xp &&
    parseFloat(infojob.salary_min) <= parseFloat(apps[i].pre_salary)  &&
    parseFloat(infojob.salary_max) >= parseFloat(apps[i].pre_salary))
   { aprovados.push(apps[i]); }

However, if you observe, this if will not be true because the last condition is false:

parseFloat(infojob.salary_max) >= parseFloat(apps[i].pre_salary)
                  |                                 |
               1500.01                           1500.12

1500.01 is not >= than 1500.12.


Example where all conditions are true:

var data = {
   data: [{
      distance: 1,
      escolaridade: "TecnologoCursando",
      user: "[email protected]",
      pre_salary: "1500.12",
      years_xp: 3
   }],
   datas: {
      distance: 2,
      xp: 3,
      salary_max: "1500.12",
      salary_min: "200.00"
   }
}

var apps = data.data;
console.log(apps[0]);
var infojob = data.datas;
console.log(infojob);
var aprovados = [];
for(i = 0; i < apps.length; i++){
   if (infojob.distance >= apps[i].distance  &&
       infojob.xp <= apps[i].years_xp &&
       parseFloat(infojob.salary_min) <= parseFloat(apps[i].pre_salary)  &&
       parseFloat(infojob.salary_max) >= parseFloat(apps[i].pre_salary))
   { aprovados.push(apps[i]); }
}
console.log(aprovados);

  • Thanks man, you helped me a lot! ...regarding the data, I have a lot more I put there just example to be able to explain more or less what needs rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.