How to do javascript PROCV (excel) function

Asked

Viewed 1,115 times

0

How to do excel Procv function in javascript;

Ex:

I have a table with 2 columns (name and code) and I have a list with only names, I need to go through this list with names and bring the code corresponding to each name according to my table cited above .

Table

Nome    Codigo
João    340
Lucas   200
Tiago   120

List

['João','Lucas','Tiago']

I need to bring the codes corresponding to each name on that list .


I have a impassable in the following situation

I have 2 different json with 1 property in common. In this case both have common number. I need to verify that the Json2 number is equal to the Json 1 number, if they are equal to the Json2 and Json1 Code.

  Json1 = [{"Codigo":1233123,"Numero":12345},
            {"Codigo":5345345,"Numero":45678},
            {"Codigo":34234,"Numero":8907},
            {"Codigo":423453,"Numero":340}]

  Json2 = [{"Numero":12345,"Nome":'Tiago'},
            {"Numero":45678,"Nome":'Joao'},
            {"Numero":8907,"Nome":'Paulo'},
            {"Numero":340,"Nome":'Maria'}]
  • You look for a JS function that does what excel PROCV does, that’s it?

  • I think you will need to provide more details. Edit your question, exemplify your problem in Javascript. The way your question is, only those who know Excel can try to infer more or less what you want. Focus on the problem (if you.

  • yes Lucas Costa

  • Ok, better. I took my vote to close. Still, it would be good to make it clear what kind of table this is. Is it a JSON file? Is it an HTML table? Otherwise whoever’s interested in helping you needs guess what it is. Make your life easier by facilitating who will help you.

  • @Lucascosta would have a solution to the situation with Json ?

  • Still in trouble @Thalleshonorato? I wonder why you even answered your question with the solution.

  • @Lucascosta unfortunately I erred in some aspects in the question, it is reformulated if possible analyze , I need to make the comparison between Json ,

Show 2 more comments

2 answers

3


Starting from the principle that this is the structure of your table, the idea here is:

  • Scroll through all cells in the table
  • Since each row will have two columns, increment by two (and access the Dice x and next to get the name the code
  • Store the result in an object array

var nomes = ['João', 'Lucas', 'Tiago'];

var itens = document.getElementById("corpo").querySelectorAll("td"); // obter cada célula da tabela

// obj sera o auxiliar para criar cada objeto, e procV é o array que conterá todos os objs
var obj = {},
  procV = [];

// iteracao de dois em dois, para percorrer linha por linha (nesse caso 3)
for (var i = 0; i < itens.length; i = i + 2) {
  // reinicializa para um novo obj
  obj = {};
  obj.nome = nomes[nomes.indexOf(itens[i].innerHTML)];
  obj.valor = itens[i + 1].innerHTML;
  
  // adiciona no array de objeto
  procV.push(obj);
}

console.log(procV);
<table id="proc" border="1">
  <tr>
    <th>Nome</th>
    <th>Código</th>
  </tr>
  <tbody id="corpo">
    <tr>
      <td>Lucas</td>
      <td>200</td>
    </tr>
    <tr>
      <td>Tiago</td>
      <td>120</td>
    </tr>
    <tr>
      <td>João</td>
      <td>340</td>
    </tr>
  </tbody>
</table>

1

Using the above method by Lucas Costa I was able to make this "Procv" using a data source in Json for those who want to example follows below

var telefones = [64992072564, 64992573900, 64992824568, 64994077908, 32312323];

var data = { "teste":
              [{"Codigo":1233123,"Telefone":64992072564},
              {"Codigo":5345345,"Telefone":64992323232},
              {"Codigo":34234,"Telefone":64992573900},
              {"Codigo":423453,"Telefone":64992824568},
              {"Codigo":534,"Telefone":64993075236},
              {"Codigo":31231,"Telefone":64993325904},
              {"Codigo":5345345,"Telefone":64993576572},
              {"Codigo":312312,"Telefone":64993827240},
              {"Codigo":5345,"Telefone":64994077908},
              {"Codigo":3123,"Telefone":64994328576},
              {"Codigo":4353453,"Telefone":64994579244},
              {"Codigo":31231,"Telefone":64994829912},
              {"Codigo":645645,"Telefone":64995080580},
              {"Codigo":4234234,"Telefone":64995331248},
              {"Codigo":64564,"Telefone":64995581916},
              {"Codigo":42342,"Telefone":64995832584},
              {"Codigo":6456456,"Telefone":64996083252}]
};

var objeto = {},
    procV = [];

for (var i = 0; i < data.teste.length; i++) {

    var teste = data.teste[i];
    obj = {};
    obj.telefone = telefones[telefones.indexOf(teste.Telefone)];

    // Se houver telefones da lista telefones no meu json ele procura o codigo e faz o push 
    if(obj.telefone > 0){
         obj.codigo = teste.Codigo;
         procV.push(obj);
    }

}

console.log(procV);
  • If Lucas' answer helped you, vote for it and mark it as accepted. Don’t post it like you used it. Again, this site is not a forum. You should take a look at [help] to better understand how it works, okay? : ) It will help you in the future.

Browser other questions tagged

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