Inter-model operation

Asked

Viewed 64 times

5

I don’t know very well the power of javascript, but I wanted to do the following:

I have two models:

Vacancy:

{
    Name: "VagaExemplo",
    Description: "Descricao",
    Skills: {
        "56b68108869038280db291e6": "90",
        "56b68108869038280db291d9": "70",
    }
}

and various models of the:

People1

{
    Name: "PessoaExemplo",
    Description: "Descricao",
    Skills: {
        "56b68108869038280db291d9": "60",
    },
    Pontuacao: 10
}

People2

{
    Name: "PessoaExemplo2",
    Description: "Descricao",
    Skills: {
        "56b68108869038280db291e6": "90",
        "56b68108869038280db291d9": "80",
    },
    Pontuacao: 0
}

The idea is to multiply the value of each Skill of the vacancy with that of the person and go adding, to generate a score for face person.

Could someone help me or at least a link with a material about it? Hugs.

  • "multiply the value of each Skill of the wave with the person " or multiply with the score? as well "with the person"?

  • for example, the person also has a Skill (they are identified by the ids), I think of something like, People1.Score = 60 x 70 ( that is 4200) People2.Score = (90 x 90) + (80 x 70) (that is 13700).

1 answer

3


I think what you’re looking for is this:

var pontos = pessoas.map(function(pessoa) {
    var pontos = 0;
    Object.keys(pessoa.Skills).forEach(function(codigo) {
        pontos += parseInt(tabela.Skills[codigo], 10) * parseInt(pessoa.Skills[codigo], 10);
    });
    return pontos;
});

Assuming each person is an object within an array.

Example: https://jsfiddle.net/pvfpawn1/

If you want to also have the name of the person in the final result you can do so: https://jsfiddle.net/pvfpawn1/1/


What this code does is know how many Skills person has with Object.keys(pessoa.Skills). Then for each code of this Skill goes to the table to find the multiplier. Use parseInt 'cause those numbers are like String, If they were numbers you wouldn’t have to.

  • 1

    Man, that’s exactly what I needed. Thank you very much. Big hug.

Browser other questions tagged

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