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

96
Vistas
How to filter an array which is inside an object which has other details as well

I have an object

const object = {
 name:'XYZ',
 Others: 'XYZ',
 numbers:[2,3,4,5,6,5]
}

I want to filter numbers array which is inside the object and return the object with filtered array like this in which array has numbers 5 only.

{
 name:'XYZ',
 Others: 'XYZ',
 numbers:[5,5]
}

If I use filter in JavaScript like this

object.numbers.filter()

It returns only numbers array not complete object with other details like name. What is the best way to implement it?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just assign object.numbers with filter return like:

const object = {
 name:'XYZ',
 Others: 'XYZ',
 numbers:[2,3,4,5,6,5]
};
object.numbers = object.numbers.filter(el => el === 5);
console.log(object);

about 4 years ago · Juan Pablo Isaza Denunciar

0

to make it work with this.setState you have to create a new Object:

let object = {
 name:'XYZ',
 Others: 'XYZ',
 numbers:[2,3,4,5,6,5]
}

let updatedObject = {...object,numbers:object.numbers.filter(e => e === 5)}
console.log(updatedObject)
// this.setState(updateObject)

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