考虑到一个对象:

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

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

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

当前回答

在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"

其他回答

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

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: {}}}

const obj = { foo: "bar" };

delete obj.foo;
obj.hasOwnProperty("foo"); // false

arr;             // [0, 1, 2, 3, 4]
arr.splice(3,1); // 3
arr;             // [0, 1, 2, 4]

let parent = {
    member: { str: "Hello" }
};
let secondref = parent.member;

delete parent.member;
parent.member;        // undefined
secondref;            // { str: "Hello" }

另一个重要缺点是,删除操作员不会为您重新组织结构,这有可能看起来反直觉的结果。

let array = [0, 1, 2, 3]; // [0, 1, 2, 3]
delete array[2];          // [0, 1, empty, 3]

let fauxarray = {0: 1, 1: 2, length: 2};
fauxarray.__proto__ = [].__proto__;
fauxarray.push(3);
fauxarray;                // [1, 2, 3]
Array.isArray(fauxarray); // false
Array.isArray([1, 2, 3]); // true

任何方法使用 Symbol.iterator 将返回不确定的值在指数. forEach,地图和减少将简单地错过错过的指数,但不会删除它

例子:

let array = [1, 2, 3]; // [1,2,3]
delete array[1];       // [1, empty, 3]
array.map(x => 0);     // [0, empty, 0]

Array#splice 改变序列,并返回任何被删除的指标. deleteCount 元素从指标开始移除,并且 item1, item2... itemN 从指标开始插入序列. 如果 deleteCount 被忽略,则从 startIndex 元素被移除到序列结束。

let a = [0,1,2,3,4]
a.splice(2,2) // returns the removed elements [2,3]
// ...and `a` is now [0,1,4]

首頁 〉外文書 〉文學 〉西洋文學 〉Array#slice([start[, end])

let a = [0,1,2,3,4]
let slices = [
    a.slice(0,2),
    a.slice(2,2),
    a.slice(2,3),
    a.slice(2,5) ]

//   a           [0,1,2,3,4]
//   slices[0]   [0 1]- - -   
//   slices[1]    - - - - -
//   slices[2]    - -[3]- -
//   slices[3]    - -[2 4 5]

首頁 #pop

交换#Shift

@johnstock,我们也可以使用JavaScript的原型化概念,将方法添加到对象中,以删除在呼叫对象中可用的任何过渡密钥。

上面的答案值得欣赏。

var myObject = {“ircEvent”:“PRIVMSG”,“方法”:“newURI”,“regex”:“^http://.*” }; // 1st 和直接方式删除 myObject.regex; // 删除 myObject[“regex”] console.log(myObject); // { ircEvent:‘PRIVMSG’,方法:‘newURI’ } // 2 方式 - 使用 JavaScript 的原型概念 Object.prototype.removeFromObjectByKey = 函数(关键) { // 如果有,删除它并删除它

使用 ramda#dissoc 您将获得一个新的对象,而无需属性 regex:

const newObject = R.dissoc('regex', myObject);
// newObject !== myObject

你也可以使用其他功能来实现相同的效果 - 忽略,选择,...

JavaScript 中的对象可以被认为是关键与值之间的地图. 删除操作员用于删除这些关键,更常见地称为对象属性,一次。

var obj = { myProperty: 1 } console.log(obj.hasOwnProperty('myProperty')) // true delete obj.myProperty console.log(obj.hasOwnProperty('myProperty')) // false

删除操作员不直接释放记忆,而它不同于简单地将零或未定义的值分配给一个属性,其中属性本身是从对象中删除的。 请注意,如果删除的属性的值是参考类型(对象),而您的程序的另一个部分仍然保留对该对象的参考,那么该对象当然不会是垃圾收集。

删除只会对其描述者标记为可配置的属性进行操作。