使用JavaScript递归删除对象的属性和值的最快方法是什么?

wlp8pajw  于 2023-01-04  发布在  Java
关注(0)|答案(8)|浏览(139)

我需要找到从对象中删除所有$meta属性及其值的最快方法,例如:

{
  "part_one": {
    "name": "My Name",
    "something": "123",
    "$meta": {
      "test": "test123"
    }
  },
  "part_two": [
    {
      "name": "name",
      "dob": "dob",
      "$meta": {
        "something": "else",
        "and": "more"
      }
    },
    {
      "name": "name",
      "dob": "dob"
    }
  ],
  "$meta": {
    "one": 1,
    "two": 2
  }
}

应该变成如下形式,因为$meta属性可能位于对象中的任何点,所以可能需要某种形式的递归。

{
  "part_one": {
    "name": "My Name",
    "something": "123"
  },
  "part_two": [
    {
      "name": "name",
      "dob": "dob"
    },
    {
      "name": "name",
      "dob": "dob"
    }
  ]
}

任何帮助或建议将不胜感激!
谢谢大家!

lrl1mhuk

lrl1mhuk1#

一个简单的自调用函数就可以做到这一点。

function removeMeta(obj) {
  for(prop in obj) {
    if (prop === '$meta')
      delete obj[prop];
    else if (typeof obj[prop] === 'object')
      removeMeta(obj[prop]);
  }
}
var myObj = {
  "part_one": {
    "name": "My Name",
    "something": "123",
    "$meta": {
      "test": "test123"
    }
  },
  "part_two": [
    {
      "name": "name",
      "dob": "dob",
      "$meta": {
        "something": "else",
        "and": "more"
      }
    },
    {
      "name": "name",
      "dob": "dob"
    }
  ],
  "$meta": {
    "one": 1,
    "two": 2
  }
}

function removeMeta(obj) {
  for(prop in obj) {
    if (prop === '$meta')
      delete obj[prop];
    else if (typeof obj[prop] === 'object')
      removeMeta(obj[prop]);
  }
}

removeMeta(myObj);

console.log(myObj);
8yparm6h

8yparm6h2#

正如@floor上面评论的:
第一个月

ma8fv8wu

ma8fv8wu3#

// Helper function
function removeProps(obj,keys){
  if(Array.isArray(obj)){
    obj.forEach(function(item){
      removeProps(item,keys)
    });
  }
  else if(typeof obj === 'object' && obj != null){
    Object.getOwnPropertyNames(obj).forEach(function(key){
      if(keys.indexOf(key) !== -1)delete obj[key];
      else removeProps(obj[key],keys);
    });
  }
}
// The object we want to iterate
var obj = {
  "part_one": {
    "name": "My Name",
    "something": "123",
    "$meta": {
      "test": "test123"
    }
  },
  "part_two": [
    {
      "name": "name",
      "dob": "dob",
      "$meta": {
        "something": "else",
        "and": "more"
      }
    },
    {
      "name": "name",
      "dob": "dob"
    }
  ],
  "$meta": {
    "one": 1,
    "two": 2
  }
};
// Utilize the utility
removeProps(obj,['$meta']);
// Show the result
document.body.innerHTML = '<pre>' + JSON.stringify(obj,null,4) + '</pre>';
8mmmxcuj

8mmmxcuj4#

任意键位于对象中的任意级别时,我使用**@Joseph Marikle**的引用函数创建了此函数

const _ = require("lodash");
const isObject = obj => obj != null && obj.constructor.name === "Object";
const removeAttrDeep = (obj, key) => {
    for (prop in obj) {
      if (prop === key) delete obj[prop];
      else if (_.isArray(obj[prop])) {
        obj[prop] = obj[prop].filter(k => {
          return !_.isEmpty(removeAttrDeep(k, key));
        });
     } else if (isObject(obj[prop])) removeAttrDeep(obj[prop], key);
    }
    return obj;
 };

示例:

const _obj = {
       a: "b", b: "e", c: { a: "a", b: "b", c: "c"},
       d: [ { a: "3" }, { b: ["2", "3"] }]};
 console.log(removeAttrDeep(_obj, "b"));
cvxl0en2

cvxl0en25#

(抱歉,我还没有足够的信誉点直接评论。
仅供参考,type of null ==='object',因此在@joseph-marikle提供的removeMeta()示例中,函数将递归空值。
在此阅读更多信息:why is typeof null "object"?

zaqlnxep

zaqlnxep6#

下面是一个函数,它接受一个字符串或一个字符串数组来递归移除(基于Joseph的答案):

// removes all propsToRemove (passed as an array or string), drilling down up to maxLevel times
// will modify the input and return it
du.removeAllPropsFromObj = function(obj, propsToRemove, maxLevel) {
    if (typeof maxLevel !== "number") maxLevel = 10
    for (var prop in obj) {
        if (typeof propsToRemove === "string" && prop === propsToRemove)
            delete obj[prop];
        else if (propsToRemove.indexOf(prop) >= 0)      // it must be an array
            delete obj[prop];
        else if (typeof obj[prop] === "object" && maxLevel>0)
            du.removeAllPropsFromObj(obj[prop], propsToRemove, maxLevel-1);
    }
    return obj
}
xu3bshqb

xu3bshqb7#

// recursively delete a key from anywhere in the object
// will mutate the obj - no need to return it
const deletePropFromObj = (obj, deleteThisKey) => {
  if (Array.isArray(obj)) {
    obj.forEach(element => deletePropFromObj(element, deleteThisKey))
  } else if (typeof obj === 'object') {
    for (const key in obj) {
      const value = obj[key]
      if (key === deleteThisKey) delete obj[key]
      else deletePropFromObj(value, deleteThisKey)
    }
  }
}

deletePropFromObj(obj, '$meta');
nhhxz33t

nhhxz33t8#

这样可以避免循环引用的任何问题

const recursiveRedact = <T extends Object>(obj: T, keys: Set<string>, visitedIn?: Set<any>): T => {
  if (obj == null || typeof obj !== 'object') return obj;
  const visited = visitedIn ?? new Set();
  visited.add(obj);
  Object.entries(obj).forEach(([key, val]) => {
    if (keys.has(key)) {
      obj[key as keyof T] = '[redacted]' as any;
    } else if (typeof val === 'object' && !visited.has(val)) {
      recursiveRedact(val, keys, visited);
    }
  });
  return obj;
};

相关问题