Query with javaScript (EcmaScript)

0

I have the following objects and I need to check which candidates meet the requested job.

var vaga = {"language":"javascript","courses":["information systems","programming"]}

var candidatos = 
[
{"name":"luis","course":"programming","languages":["c","javascypt"]},{"name":"ana","course":"information systems","languages": "php","javascript","java"]},{"name":"pedro","course":"programming","languages":["javascript","java"]},{"name":"paulo","course":"programming","languages":["javascript","php","java"]},
{"name":"barbara","course":"information systems","languages":["php","javascript"]},
{"name":"camila","course":"programming","languages":["java","javascript"]},{"name":"cesar","course":"fashion","languages":["java","javascript","c"]},{"name":"cleber","course":"programming","languages":["reggae","javascript","dogs","football cards"]},{"name":"bruno","course":"programming","languages":["regex","javascript","perl","go","java"]},
{"name":"joana","course":"web design","languages":["java","javascript","c"]}
]

For this, I created the function below that returns an array with all the candidates, but I wanted to do this using EcmaScript, but I'm not sure how I would do it using filter() , map() and reduce() .

function retornaCandidatos(vaga, candidatos) {
    var x = [];
    for (var i = 0; i < candidatos.length ; i++) {
        for(var j = 0; j < vaga.courses.length; j++){
            if(candidatos[i].course === oportunidade.courses[j]){
                for(var k = 0; k < candidatos[i].languages.length; k++){
                    if(vaga.language === candidatos[i].languages[k]){
                        x.push(candidatos[i]);
                    }
                }
            }
        }
    }
    return x;
}
    
asked by anonymous 22.11.2018 / 19:18

4 answers

2

Hello, how are you?

I would do it as follows

let vaga = {"language":"javascript","courses":["information systems","programming"]}

let candidatos = [
  {"name":"luis","course":"programming","languages":["c","javascypt"]},
  {"name":"ana","course":"information systems","languages": ["php","javascript","java"]},
  {"name":"pedro","course":"programming","languages":["javascript","java"]},
  {"name":"paulo","course":"programming","languages":["javascript","php","java"]},
  {"name":"barbara","course":"information systems","languages":["php","javascript"]},
  {"name":"camila","course":"programming","languages":["java","javascript"]},
  {"name":"cesar","course":"fashion","languages":["java","javascript","c"]},
  {"name":"cleber","course":"programming","languages":["reggae","javascript","dogs","football cards"]},
  {"name":"bruno","course":"programming","languages":["regex","javascript","perl","go","java"]},
  {"name":"joana","course":"web design","languages":["java","javascript","c"]}
]

const retornaCandidatos = (vaga, candidatos) => {
  return candidatos.filter(candidato => {
    return candidato.languages.some(lang => vaga === lang)
  })
}

let aprovados = retornaCandidatos('php', candidatos)

console.log('Aprovados: ', aprovados)

Summary of changes made to the code:

Where you put var I changed to let , in new versions of EcmaScript we also have const available, maybe it fits better in some situation for you, used in this case.

The difference in this issue is mainly in the scope , you can see more #, but in summary and rude form var has global or function scope, while let is "local" scope, const is similar to let , however is used to define constants.

The second major change was in the form of declaring the function, for this I used arrow function , you can see more here . Again in a nutshell, it's a simpler way to declare functions.

And in the function I used two well-used native methods these days. The filter and some .

Once again in a brief and rude way, filter returns the array object in which some condition is satisfied, learn more

22.11.2018 / 19:45
0

Maybe this change in function will help you, it got much smaller and, in my opinion, a lot easier to understand:

function retornaCandidatos(vaga, candidatos) {
        var x = 
        candidatos.filter(function(candidato){

           //Retorna true caso o a vaga.language exista na lista de linguagens do candidato e o curso feito pelo candidato estiver na lista de cursos da vaga
           //DICA: Caso queira que os candidatos no array só precise atender uma das condições, basta trocar o and(&&) por or(||)

    		return candidato.languages.indexOf(vaga.language) > -1 && vaga.courses.indexOf(candidato.course) > -1
    	})
        return x;
    }
    var vaga = {"language":"javascript","courses":["information systems","programming"]}
    
    var candidatos = 
    [
    {"name":"luis","course":"programming","languages":["c","javascypt"]},{"name":"ana","course":"information systems","languages": [ "php","javascript","java"]},{"name":"pedro","course":"programming","languages":["javascript","java"]},{"name":"paulo","course":"programming","languages":["javascript","php","java"]},
    {"name":"barbara","course":"information systems","languages":["php","javascript"]},
    {"name":"camila","course":"programming","languages":["java","javascript"]},{"name":"cesar","course":"fashion","languages":["java","javascript","c"]}, {"name":"cleber","course":"programming","languages":["reggae","javascript","dogs","football cards"]},{"name":"bruno","course":"programming","languages":["regex","javascript","perl","go","java"]},
    {"name":"joana","course":"web design","languages":["java","javascript","c"]}
    ];
    console.log(retornaCandidatos(vaga, candidatos))
  

link

    
22.11.2018 / 19:41
0

Hello, follows response using native function "filter" as requested. It gets more elegant and easier to understand. Note: Your candidate array had a syntax error that I fixed.

var vaga = { "language": "javascript", "courses": ["information systems", "programming"] };

        var candidatos =
                [{ "name": "luis", "course": "programming", "languages": ["c", "javascypt"] },
                { "name": "ana", "course": "information systems", "languages": ["php", "javascript", "java"] },
                { "name": "pedro", "course": "programming", "languages": ["javascript", "java"] },
                { "name": "paulo", "course": "programming", "languages": ["javascript", "php", "java"] },
                { "name": "barbara", "course": "information systems", "languages": ["php", "javascript"] },
                { "name": "camila", "course": "programming", "languages": ["java", "javascript"] },
                { "name": "cesar", "course": "fashion", "languages": ["java", "javascript", "c"] },
                { "name": "cleber", "course": "programming", "languages": ["reggae", "javascript", "dogs", "football cards"] },
                { "name": "bruno", "course": "programming", "languages": ["regex", "javascript", "perl", "go", "java"] },
                { "name": "joana", "course": "web design", "languages": ["java", "javascript", "c"] }];



        let retornaCandidatos = candidatos.filter(item => {
                if ((item.course == vaga.courses[0] || item.course == vaga.courses[1]) &&
                        item.languages.filter(item2 => {
                                if (item2 == vaga.language)
                                        return true;
                        }))
                        return item;
        });

        console.log(retornaCandidatos);
    
22.11.2018 / 19:42
0

I want to thank everyone who showed me solutions to my question, that's what I needed, a way to go through an array without using "for" loop, only using filter () or map (). >     

23.11.2018 / 21:53