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

135
Vistas
Having an issue converting an object to a filtered array

I'd like to filter and restructure some specific data, but I'm having some trouble getting it to produce the proper structure.

I have this data response in this structure....

var response = {
   badProperty1: "bad property text"
   goodProperty1: "abc"
   goodProperty2: "bcd"
   goodProperty3: "cde"
   goodProperty4: "def"
   goodProperty5: "efg"
   badProperty2: "fgh"
};

I'd like to convert my response object into an array like this structure exactly...

var newFilteredStructuredArray = [
   {goodProperty1: 'abc'},
   {goodProperty2: 'bcd'},
   {goodProperty3: 'cde'},
   {goodProperty4: 'def'},
   {goodProperty5: 'fgh'}
];

However I'd like to filter out the bad properties before I get my new array. I'd like my array to only include these specific property names and filter everything else that I started with.

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

an implementation using filter and map on the object entries of response. In the filter stage I filter out only if it is available in possiblePropertyNames. Then in the map stage I return the array in the format you need

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
var response = {badProperty1: "bad property text",goodProperty1: "abc",goodProperty2: "bcd",goodProperty3: "cde",goodProperty4: "def",goodProperty5: "efg",badProperty2: "fgh"}

var newFilteredStructuredArray = Object.entries(response).filter(([k,v]) => possiblePropertyNames.includes(k)).map(([k,v]) => ({[k]:v}))

console.log(newFilteredStructuredArray)

without es6

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
var response = {badProperty1: "bad property text",goodProperty1: "abc",goodProperty2: "bcd",goodProperty3: "cde",goodProperty4: "def",goodProperty5: "efg",badProperty2: "fgh"}

var newFilteredStructuredArray = Object.keys(response)
.filter(function(a){
    return possiblePropertyNames.includes(a)
})
.map(function(b){
    return {[b]:response[b]}
})

console.log(newFilteredStructuredArray)

UPDATE better answer with just a map on the possiblePropertyNames array as pointed out by Andreas in the comments

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
var response = {badProperty1: "bad property text",goodProperty1: "abc",goodProperty2: "bcd",goodProperty3: "cde",goodProperty4: "def",goodProperty5: "efg",badProperty2: "fgh"}

var newFilteredStructuredArray = possiblePropertyNames.map(prop => ({[prop]:response[prop]}))

console.log(newFilteredStructuredArray)

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