Query with Javascript

Asked

Viewed 81 times

0

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

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 candidates, however, I wanted to do this using filter(), map() and reduce(), but I don’t know how.

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;
}
  • 2

    Are you having a problem? This is Ecmascript. Some reason to change a routine that I believe is working well, you know and is efficient for another that you don’t know and is inefficient?

  • Where it comes from opportunity.?

  • @Maniero I think it should be for study reasons anyway, I think that for this *script * to be somewhat simple, maybe he has done an old course and wants to update himself.

  • @Maniero, this was actually an exercise I needed to do, but I couldn’t use for, so I was looking for a way to do it using filter() or map().

  • @Luis usually exercises that prohibit using for ask to use another flow control, that is, reduce abstraction and not increase. Did the exercise make this clear? If I didn’t, the exercise is flawed.

  • @Maniero, didn’t make it clear, just said to use filter(), map() or reduce()

Show 1 more comment

4 answers

2

Hello, all right?

I’d do it this way

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 did you put var I traded for let, in the new versions of Ecmascript we also have available the const, maybe it fits better in some situation for you, even could be used in this case.

The difference in this question is mainly in the scope, you can see more in that question, but in a brief and rude way var has scope global or of function, as long as the let has "local" scope, the const in turn is similar to let, but is used to define constants.

The second major change was in the way to declare the function, for this I used Arrow Function , you can see more here. Again summarized and rude, it is a more simplified way of declaring functions.

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

Once again summarized and rude, filter returns the array object in which some condition is satisfied, learn more here, and the some if any item of the array is satisfied it returns true, otherwise false, you can see more about it here.

Bonus If you want something more dynamic, such as filtering candidates while someone types in a input, you could create a RegExp (Regular Expression) simple. And implement 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) => {
  let exp = new RegExp(vaga.trim(), 'i')

  return candidatos.filter(candidato => {
    return candidato.languages.some(lang => exp.test(lang))
  })
}


let aprovados = retornaCandidatos('java', candidatos)

console.log('Aprovados: ', aprovados)

In that case it will return programmers java and javascript, for with that Regexp, he nay will Differentiate capital from minuscule and it still won’t matter if the search word is in the beginning, middle or end of the string, just contain that snippet to return it. Remembering this would be useful in a dynamic search, maybe not your case.

  • 1

    Eduardo Ribeiro, thanks for your help!

  • In need, we are here to help :D

1

Perhaps this modification in the function will help you, it has become much smaller and, in my opinion much 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))

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filtro

  • Edson Alves, thanks for your help!

1


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

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);

  • Adjair Costa, thanks for your help!

0

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

Browser other questions tagged

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