Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
Properly filter names on an array in java

so I have this data on an array and I need to only appear [ {apellido_paterno:"algo",nombre: "jose"} ] in the output but its showing all 3 of the objects

let data = [{nombre: 'joselyn',apellido_paterno:'poblete'},
{nombre:'jose',apellido_paterno:'algo'},
{nombre:'ernesto',apellido_paterno:'joseludo'}]
const filtro = 'jose'

data = data.filter((row) =>{
  const nombreCompleto = `${row.nombre}${row.apellido_paterno}`.toLowerCase()
  const nombreCompletoReves = `${row.apellido_paterno}${row.nombre}`.toLowerCase()
  const nombreTrimmed = filtro.replace(/\s+/g, '').toLowerCase()
  return nombreCompleto.includes(nombreTrimmed) || nombreCompletoReves.includes(nombreTrimmed);
})

console.log(data) 
// expected output  [{apellido_paterno:"algo",nombre: "jose"}]
// output [{apellido_paterno: "poblete",nombre: "joselyn"},{apellido_paterno:"algo",nombre: "jose"},
// {apellido_paterno: "joseludo",nombre: "ernesto"}]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If I understand correctly, you want to filter the object where nombre is jose. You can just .filter() on data. Like this

let data = [{
    nombre: 'joselyn',
    apellido_paterno: 'poblete'
  },
  {
    nombre: 'jose',
    apellido_paterno: 'algo'
  },
  {
    nombre: 'ernesto',
    apellido_paterno: 'joseludo'
  }
]
const filtro = 'jose'

data = data.filter(x => x.nombre == filtro);

console.log(data)

about 4 years ago · Juan Pablo Isaza Report

0

Here is the working code

let data = [
  { nombre: "joselyn", apellido_paterno: "poblete" },
  { nombre: "jose", apellido_paterno: "algo" },
  { nombre: "ernesto", apellido_paterno: "joseludo" },
  // line added by me to demonstrate the first example
  { nombre: "javier", apellido_paterno: "jose rodrigo" }
];
const filtro = "jose";

console.log("filtro wrapped by spaces in any part of row.nombre or row.apellido_paterno",
  data.filter(row => 
    (row.nombre + " " + row.apellido_paterno)
      .toLowerCase()
      .split(" ")
      .includes(filtro)
  )
);

console.log("filtro strictly matches row.nombre or row.apellido_paterno",
  data.filter(row => 
    row.nombre == filtro ||
    row.apellido_paterno == filtro
  )
);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!