Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop an object and remove all functions from it

Let's get right the issue, I need to remove all functions from a object to send via socket.io JSON! Let's say I have an object such as...

let myObject = {
    test1: "abc",
    test2: function() {
        console.log("This is a function");
    },
    test3: {
        test4: "another string",
        test5: function() {
            console.log("another function");
        },
        test6: 123
    }
}

and I need to convert it too

let myObject = {
    test1: "abc",
    test3: {
        test4: "another string",
        test6: 123
    }
}

I have tried many cod methods, all of which failed! I would post them but that would be wasting your valuable time. I'd love anyone who could fix this problem in a neat function manner. Yours Truly, Jacob Morris

P.S. Here is a piece of c**p attempt at doing this

let myObject = {
  test1: "abc",
  test2: function() {
      console.log("This is a function");
  },
  test3: {
      test4: "another string",
      test5: function() {
          console.log("another function");
      },
      test6: 123
  }
}

let i = [];
function tsfr(ls) {
    let i = {};
    for (let a in ls) {
        if (typeof(ls[a]) !== "function") {
            let d = ls[a];
            if (typeof(d) == "object") {
                d = tsfr(d);
            }
            i[a] = d;
        }
    }
    return i;
}
i = tsfr(myObject);
console.log(i)
like image 589
Jacob Morris Avatar asked Oct 21 '25 14:10

Jacob Morris


1 Answers

You can write your own recursive solution, but I think it is better to use JSON.stringify. By default stringify ignores functions and removes them from the result. In certain cases, it's second param, replacer function, can get handy for more advanced object manipulation.

const removeFunctions = (obj) => {
  const stringified = JSON.stringify(obj);

  // We need to parse string back to object and return it
  const parsed = JSON.parse(stringified);

  return parsed;
}

const myObject = {
    test1: "abc",
    test2: function() {
        console.log("This is a function");
    },
    test3: {
        test4: "another string",
        test5: function() {
            console.log("another function");
        },
        test6: 123
    }
}

console.log(removeFunctions(myObject));

(Or on codepen)

Please note that stringifying uses toString() method, and for some instances of custom classes can lead to a data loss. To prevent that, you'll need to write your own, recursive solution. It is probably going to be a lot more complicated.

Hope this helps, cheers!

EDIT: I just saw your attempt. It is a step in right direction, but it needs some more love. But I need to advise you against variable names like tsfr or ls. Code is much more readable if you use longer, more descriptive names.

EDIT2: As pointed by Andreas in the comments, you don't even need custom replacer as stringify ignores them and removes them by default.

like image 125
Stanko Avatar answered Oct 23 '25 03:10

Stanko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!