Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
how to search inner array inside a json and return the same error with filtered data

I have an array of object and inside every array i have list of users, when i search i should be able to search the users and return value without effecting the existing structure. stackblitz

I have tried but its not working have shared the stackblitz link where I have done

userList = [
    { state: 'KL', level: 1, USERS: ['jane', 'kete'] },
    { state: 'TN', level: 1, USERS: ['john', 'rock'] },
    { state: 'MH', level: 2, USERS: ['rahul', 'ricky'] },
    { state: 'AP', level: 2, USERS: ['ram', 'sham'] },
  ];

handleSearch(kete) // search us an input field detailed in stackblitz link
handleSearch = (e) => {
    this.userList = this.orgList;
    const user = this.searchFunction(this.userList, e.target.value);
    console.log(user);
  };

  // search function
  searchFunction = (arr, searchVal) => {
    return arr.map((item) => {
      console.log(item);
      const data = item.USERS.filter((user) =>
        user.toLowerCase()?.includes(searchVal?.toLowerCase())
      );
      return { ...item, data };
    });
  };
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use filter() and includes() to filter out the objects where the username is in the USERS array of the object.

const userList = [
  { state: 'KL', level: 1, USERS: ['jane', 'kete'] },
  { state: 'TN', level: 1, USERS: ['john', 'rock'] },
  { state: 'MH', level: 2, USERS: ['rahul', 'ricky'] },
  { state: 'AP', level: 2, USERS: ['ram', 'sham'] },
];
  
const findUser = (username, users) => {
  return users.filter(u => u.USERS.includes(username));
}

console.log(findUser("rahul", userList));

about 4 years ago · Juan Pablo Isaza Denunciar

0

As you are returning data in an object through { ...item, data }, the retains all it's fields and adds a new field called data with filtered users.

Instead what you want to do is replace the list of USERS in the current item object.

  searchFunction = (arr, searchVal) => {
    return arr.map((item) => {
      const data = item.USERS.filter((user) =>
        user.toLowerCase()?.includes(searchVal?.toLowerCase())
      );
      return { ...item, USERS: data };
    });
  };

Stackblitz

about 4 years ago · Juan Pablo Isaza Denunciar

0

Check this solution: https://stackblitz.com/edit/angular-w1vi9e Basicly, we make an array of users, and on each value of search input change, we search the array of users for any user whose name contains the search value and we're showing their names for testing purposes.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda