考虑到一个对象:

let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI",
  "regex": "^http://.*"
};

如何删除财产 regex 以完成下一个 myObject?

let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI"
};

当前回答

一个没有财产的物体的克隆:

例如:

let object = { a: 1, b: 2, c: 3 };

我们必须删除A。

有一个明确的 prop 密钥: const { a,...rest } = 对象; 对象 = 休息; 有一个变量的 prop 密钥: const propKey = 'a'; const { [propKey]: propValue,...rest } = 对象; 对象 = 休息; A cool arrow 函数 <unk>: const removeProperty = (propKey, { [propKey]: propValue,...rest }) => 休息; 对象 = removeProperty('a', 对象); 多种属性 const removeProperties = (object,...keys)

使用

object = removeProperties(object, 'a', 'b') // result => { c: 3 }

const propsToRemove = ['a', 'b']
object = removeProperties(object, ...propsToRemove) // result => { c: 3 }

其他回答

在JavaScript中删除财产

突变物品属性删除,不安全

此类别适用于使用对象字母或对象例子时,您希望保持/继续使用原始参考,并且在您的代码中不使用无标志的功能原则。

'use strict'
const iLikeMutatingStuffDontI = { myNameIs: 'KIDDDDD!', [Symbol.for('amICool')]: true }
delete iLikeMutatingStuffDontI[Symbol.for('amICool')] // true
Object.defineProperty({ myNameIs: 'KIDDDDD!', 'amICool', { value: true, configurable: false })
delete iLikeMutatingStuffDontI['amICool'] // throws

基于回归的链条财产遗弃

此类别适用于在更新的 ECMAScript 品味中运行平面对象或序列示例,当不变化的方法是需要的,并且您不需要为符号密钥负责:

const foo = { name: 'KIDDDDD!', [Symbol.for('isCool')]: true }
const { name, ...coolio } = foo // coolio doesn't have "name"
const { isCool, ...coolio2 } = foo // coolio2 has everything from `foo` because `isCool` doesn't account for Symbols :(

突变物品属性删除,安全

'use strict'
const iLikeMutatingStuffDontI = { myNameIs: 'KIDDDDD!', [Symbol.for('amICool')]: true }
Reflect.deleteProperty(iLikeMutatingStuffDontI, Symbol.for('amICool')) // true
Object.defineProperty({ myNameIs: 'KIDDDDD!', 'amICool', { value: true, configurable: false })
Reflect.deleteProperty(iLikeMutatingStuffDontI, 'amICool') // false

const foo = { name: 'KIDDDDD!', [Symbol.for('isCool')]: true }
const { name, ...coolio } = foo // coolio doesn't have "name"
const { isCool, ...coolio2 } = foo // coolio2 has everything from `foo` because `isCool` doesn't account for Symbols :(

基于图书馆的财产遗弃

这个类别通常允许更大的功能灵活性,包括对符号的会计 & 在一个声明中忽略多个属性:

const o = require("lodash.omit")
const foo = { [Symbol.for('a')]: 'abc', b: 'b', c: 'c' }
const bar = o(foo, 'a') // "'a' undefined"
const baz = o(foo, [ Symbol.for('a'), 'b' ]) // Symbol supported, more than one prop at a time, "Symbol.for('a') undefined"

要从对象中删除一个属性(指向对象),你可以这样做:

delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];

Demo var myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; 删除 myObject.regex; console.log(myObject);

对于任何有兴趣阅读更多关于它的人来说,Stack Overflow 用户 Kangax 写了一篇关于他们博客的删除声明的令人难以置信的深入博客帖子,理解删除。

如果你想要一个新的对象,除了一些,所有原始的钥匙,你可以使用破坏。

我用Lodash的“Unset”来让它发生在一个被遗弃的对象中......这只需要写小逻辑来找到由遗弃方法所期望的财产密钥的路径。

返回财产路径作为序列的方法

var a = {"bool":{"must":[{"range":{"price_index.final_price":{"gt":"450", "lt":"500"}}}, {"bool":{"should":[{"term":{"color_value.keyword":"Black"}}]}}]}}; function getPathOfKey(object,key,currentPath, t){ var currentPath = currentPath || []; for(var i in object){ if(i == key){ t = currentPath; } else if(typeof object[i] == "object"){ currentPath.push(i) return getPathOfKey(object[i], key,currentPath) } } t.push(key); return t; } document.getElementById("output").innerHTML =JSON.stringify(getPathOfKey(a,"price_index.final_price")) <div id="output"> </div>

然后只使用Lodash unset方法从对象中移除财产。

var unset = require('lodash.unset'); unset(a, getPathOfKey(a,“price_index.final_price”));

如果您想删除对物体深沉的财产,那么您可以使用下列重复功能,路径到财产作为第二个论点:

var deepObjectRemove = function(obj, path_to_key){
    if(path_to_key.length === 1){
        delete obj[path_to_key[0]];
        return true;
    }else{
        if(obj[path_to_key[0]])
            return deepObjectRemove(obj[path_to_key[0]], path_to_key.slice(1));
        else
            return false;
    }
};

例子:

var a = {
    level1:{
        level2:{
            level3: {
                level4: "yolo"
            }
        }
    }
};

deepObjectRemove(a, ["level1", "level2", "level3"]);
console.log(a);

//Prints {level1: {level2: {}}}

您可以使用 Delete Property 删除对象中的财产[键]