How to make PROCV function (excel) in javascript

0

How to do the Procv function of excel 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 quoted above.

Table

Nome    Codigo
João    340
Lucas   200
Tiago   120

List

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

I need to bring the corresponding codes to each name in this list.

I have a bottleneck in the following situation

I have 2 different json's with 1 property in common. In this case both have a number in common. I need to check if the Json2 number is the same as Json 1, if they are the same I need to display Number and Name of Json2 and Code of Json1.

  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'}]
    
asked by anonymous 24.01.2017 / 17:49

2 answers

3

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

  • Scroll through all table cells
  • Since each row will have two columns, increment by two (and access the index x and next to get the name the code
  • Storing the result into an array of objects

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>
    
24.01.2017 / 19:21
0

Using the above method by Lucas Costa I was able to do this "ProcV" using a data source in Json for anyone who wants example below 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);
    
25.01.2017 / 01:31