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

157
Vistas
JavaScript - delete object properties in array of objects

Is there a better / shorter method to delete properties from objects in an array of objects than the below example. I can use vanilla JS or lodash.

Exmaple function:

  function stripObjProps(arr) {
    let newArr = _.clone(arr);
    for (let i = 0; i < arr.length; i += 1) {
      delete newArr[i].isBounded;
      delete newArr[i].isDraggable;
      delete newArr[i].isResizable;
      delete newArr[i].maxH;
      delete newArr[i].maxW;
      delete newArr[i].minH;
      delete newArr[i].minW;
      delete newArr[i].resizeHandles;
      delete newArr[i].moved;
      delete newArr[i].static;
    }
    return newArr;
  } 
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use omit from Lodash to exclude properties:

function stripObjProps(arr) {
    return arr.map(item => _.omit(item, ['isBounded', 'isDraggable', 'isResizable', 'maxH', 'maxW', 'minH', 'minW', 'resizeHandles', 'moved', 'static']));
}

const newArray = stripObjProps(originalArray)

Additionally, you can use pick instead of omit. In case of pick you specify only the properties which you want to keep.

about 4 years ago · Santiago Trujillo Denunciar

0

I can think of two ways

function stripObjProps(arr) {
    let newArr = _.clone(arr);
    for (let i = 0; i < newLay.length; i += 1) {
        [
            "isBounded", 
            "isDraggable", 
            "isResizable", 
            "maxH", 
            "maxW", 
            "minH", 
            "minW", 
            "resizeHandles", 
            "moved", 
            "static"
        ].forEach(k => delete newArr[i][k]);
    }
}

or - assuming newLay is a typo

function stripObjProps(arr) {
    return arr.map(item => {
        let {
            isBounded,
            isDraggable,
            isResizable,
            maxH,
            maxW,
            minH,
            minW,
            resizeHandles,
            moved,
            static,
            ...ret
        } = item;
        return ret;
    });
}

NOTE: no need for _.clone in this second example, since you aren't doing a deep clone, map returns a new array with a new object (...ret)

However, I don't use lodash, so there may be an even better way with that library

about 4 years ago · Santiago Trujillo Denunciar

0

Another way to remove properties with filter and Object entries / fromEntries.

const data = [
  { a: 1, b: 2, c: 3, d: 4, e: 5 }, 
  { a: 1, b: 2, c: 3, d: 4, e: 5 }, 
  { a: 1, b: 2, c: 3, d: 4, e: 5 },
];

const excludeKeys = ['b', 'c', 'd'];

const exclude = (obj) => 
    Object.fromEntries(
        Object.entries(obj)
        .filter(([key]) => !excludeKeys.includes(key)));
    
const result = data.map(exclude);

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

about 4 years ago · Santiago Trujillo 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