0

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;
  } 
4
  • 1
    ["isBounded","isDraggable","isResizable","maxH","maxW","minH","minW","resizeHandles","moved","static"].forEach(k => delete newArr[i][k]) Commented Apr 15, 2022 at 23:44
  • @Bravo forEach is missing the index i variable i guess Commented Apr 15, 2022 at 23:45
  • @ZulfiqarAli nope ... i comes from the for loop over the newLay array (whatever that is) the code I suggested is inside the for loop Commented Apr 15, 2022 at 23:46
  • 1
    I would probably map the array and rebuild the objects (destructure either the props you want or the props you don't want). Also you can use structuredClone now for native deep cloning. Commented Apr 15, 2022 at 23:52

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I knew lodash would have to have something useful - If you're going to use lodash, you may as well read the documentation and use all of it :p
2

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

2 Comments

@pilchard hmmm ... I think the second code would still produce a different result ... the original OP's code would mutate the objects in the original array, would it not? maybe not, I'm overthinking now!!!
If clone is indeed shallow, then yes, they should mutate the originals (probably an unwanted side effect?) heh.
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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.