How to filter data from an array of objects using RegExp?

0

From the following array of objects How can I filter all emails containing the word director of the property POST, without distinguishing between uppercase or lowercase, only containing that word?

var regs = [
            {EMAIL:"[email protected]", PUESTO:"DIRECTOR"}, 
            {EMAIL:"[email protected]", PUESTO:"SEGURIDAD EXTERNA"}, 
            {EMAIL:"[email protected]", PUESTO:"SEGURIDAD EXTERNA"},
            {EMAIL:"[email protected]", PUESTO:"SUBDIRECTOR A"},   
            {EMAIL:"[email protected]", PUESTO:"BOMBERO"}, 
            {EMAIL:"[email protected]", PUESTO:"BOMBERO"},
            {EMAIL:"[email protected]", PUESTO:"SUBDIRECTOR B"}

    ]; 

So that I find an array like this:

var filterRegs =['[email protected]','[email protected]','[email protected]'];
    
asked by Víctor Hernández 30.11.2017 в 23:33
source

3 answers

2

One way to do what you propose using Javascript is the following:

const data = [
    {EMAIL:"[email protected]", PUESTO:"DIRECTOR"}, 
    {EMAIL:"[email protected]", PUESTO:"SEGURIDAD EXTERNA"}, 
    {EMAIL:"[email protected]", PUESTO:"SEGURIDAD EXTERNA"},
    {EMAIL:"[email protected]", PUESTO:"SUBDIRECTOR A"},   
    {EMAIL:"[email protected]", PUESTO:"BOMBERO"}, 
    {EMAIL:"[email protected]", PUESTO:"BOMBERO"},
    {EMAIL:"[email protected]", PUESTO:"SUBDIRECTOR B"}
]; 
let results = [];

data.forEach((e) => { 
  if (/director/i.test(e.PUESTO)) results.push(e.EMAIL) 
});

console.log(results);
    
answered by 01.12.2017 / 13:16
source
1

An alternative is to use the filter method to filter the elements and map to extract the email data.

Here are two examples, the first using regular expressions in the filtering function, and the second without using them, simply capitalizing the value of PUESTO and searching for the string "DIRECTOR":

var regs = [
    {EMAIL:"[email protected]", PUESTO:"DIRECTOR"}, 
    {EMAIL:"[email protected]", PUESTO:"SEGURIDAD EXTERNA"}, 
    {EMAIL:"[email protected]", PUESTO:"SEGURIDAD EXTERNA"},
    {EMAIL:"[email protected]", PUESTO:"SUBDIRECTOR A"},   
    {EMAIL:"[email protected]", PUESTO:"BOMBERO"}, 
    {EMAIL:"[email protected]", PUESTO:"BOMBERO"},
    {EMAIL:"[email protected]", PUESTO:"SUBDIRECTOR B"}
];
    
var filterRegs = regs
  .filter((x) => /director/i.test(x.PUESTO))
  .map((x) => x.EMAIL);
  
console.log(filterRegs);

filterRegs = regs
  .filter((x) => x.PUESTO.toUpperCase().indexOf('DIRECTOR') >= 0)
  .map((x) => x.EMAIL);
  
console.log(filterRegs);
    
answered by 01.12.2017 в 14:36
0

An alternative using javascript is the following:

  • Run the object regs and add the EMAIL if the condition (which is, find the word "director") - regardless of whether or not it is uppercase / lowercase:

Example:

var regs = [{
    EMAIL: "directorgraltest @gmail.com",
    PUESTO: "DIRECTOR"
  },
  {
    EMAIL: "testsendmail309 @gmail.com",
    PUESTO: "SEGURIDAD EXTERNA"
  },
  {
    EMAIL: "testsendmail3011 @gmail.com",
    PUESTO: "SEGURIDAD EXTERNA"
  },
  {
    EMAIL: "subdirectorgral3_test @gmail.com",
    PUESTO: "SUBDIRECTOR A"
  },
  {
    EMAIL: "contatestsendmail32107 @gmail.com",
    PUESTO: "BOMBERO"
  },
  {
    EMAIL: "contatestsendmail30187 @gmail.com",
    PUESTO: "BOMBERO"
  },
  {
    EMAIL: "subdirectorgral2_test @gmail.com",
    PUESTO: "SUBDIRECTOR B"
  }

];

// Arreglo que tendrá los emails.
var filterRegs = [];

// Recorrer los elementos que contenga "regs".
for (var i = 0; i < regs.length; i++) {

  // Verificar si cumple la condición:
  if (regs[i].PUESTO.toLowerCase().indexOf("director") != -1) {

    // Agregar el "EMAIL" si se cumplió la condición.
    filterRegs.push(regs[i].EMAIL);
  }
}

// Aquí está el objeto final:
document.getElementById('divResultados').innerHTML = "var filterRegs = " + JSON.stringify(filterRegs) + ";";
console.log(filterRegs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div id="divResultados"><i>Cargando...</i></div>
    
answered by 01.12.2017 в 00:01