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

73
Vistas
map array of objects based on set of properties

Suppose I have an object:

let array = [
  {a: 1, b: 5, c: 9},
  {a: 2, b: 6, c: 10},
  {a: 3, b: 7, c: 11},
  {a: 4, b: 8, c: 12}
];

then I have a dictionary:

const columns = [
  { key: 'a', value: 'a' },
  { key: 'b', value: 'b' },
]

I want to filter out properties that are not defined in columns.

I have tried

array.map((x) =>  ({"a": x.a, "b": x.b}))

Is there a way to use the data defined in columns instead of manually typing all the properties?

Desired output:

[
  {
    "a": 1,
    "b": 5
  },
  {
    "a": 2,
    "b": 6
  },
  {
    "a": 3,
    "b": 7
  },
  {
    "a": 4,
    "b": 8
  }
]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could map entries and get the new objects.

let
    array = [{ a: 1, b: 5, c: 9 }, { a: 2, b: 6, c: 10 }, { a: 3, b: 7, c: 11 }, { a: 4, b: 8, c: 12 }],
    columns = [{ key: 'a', value: 'a' }, { key: 'b', value: 'b' }],
    keys = columns.map(({ key }) => key),
    result = array.map(o => Object.fromEntries(keys.map(k => [k, o[k]])));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use this.

This uses just an array to hold the desired columns because I don't get why you would use a dictionary with key and value being the same.

let array = [
  { a: 1, b: 5, c: 9 },
  { a: 2, b: 6, c: 10 },
  { a: 3, b: 7, c: 11 },
  { a: 4, b: 8, c: 12 },
];

const desiredColumns = ["a", "b"];

const transformed = array.map(item => {
    const obj = {};
    desiredColumns.forEach(col => {
        if(col in item){
            obj[col] = item[col];
        }
    })
    return obj;
})

console.log(array);
console.log(transformed)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another, slightly less direct way using map() and reduce():

  1. Create an array with all the keys we'll keep
  2. Reduce the array to get the desired result
    • Add current key + value if key keep array

const array   = [{a: 1, b: 5, c: 9}, {a: 2, b: 6, c: 10}, {a: 3, b: 7, c: 11}, {a: 4, b: 8, c: 12} ];
const columns = [{ key: 'a', value: 'a' }, { key: 'b', value: 'b' }, ];

const toKeep = columns.map(({ key }) => key).flat();

const result = array.map(a => 
   Object.keys(a)
      .reduce((prev, cur) => (toKeep.includes(cur)) ? { ...prev, [cur]: a[cur] } : prev, {})
);

console.log(result);

Result:

[
  {
    "a": 1,
    "b": 5
  },
  {
    "a": 2,
    "b": 6
  },
  {
    "a": 3,
    "b": 7
  },
  {
    "a": 4,
    "b": 8
  }
]
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