考虑到一个对象:

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

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

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

当前回答

假设你有一个类似于此的对象:

var Hogwarts = {
    staff : [
        'Argus Filch',
        'Filius Flitwick',
        'Gilderoy Lockhart',
        'Minerva McGonagall',
        'Poppy Pomfrey',
        ...
    ],
    students : [
        'Hannah Abbott',
        'Katie Bell',
        'Susan Bones',
        'Terry Boot',
        'Lavender Brown',
        ...
    ]
};

删除物品财产

delete Hogwarts.staff;

delete Hogwarts['staff'];

删除 array index

现在,如果你想移除一个员工或学生,程序有点不同,因为两种属性都是由自己组成的。

Hogwarts.staff.splice(3, 1);

Hogwarts.staff.splice(Hogwarts.staff.indexOf('Minerva McGonnagall') - 1, 1);

笔记

虽然你技术上可以使用删除一个序列,使用它会导致收到错误的结果,当打电话,例如,Hogwarts.staff.length 后来。

因此,在从对象中删除值时,首先要考虑您是否正在处理对象属性,或者您是否正在处理序列值,并根据此选择适当的策略。

如果你想尝试这个,你可以使用这个Fiddle作为一个起点。

其他回答

假设你有一个类似于此的对象:

var Hogwarts = {
    staff : [
        'Argus Filch',
        'Filius Flitwick',
        'Gilderoy Lockhart',
        'Minerva McGonagall',
        'Poppy Pomfrey',
        ...
    ],
    students : [
        'Hannah Abbott',
        'Katie Bell',
        'Susan Bones',
        'Terry Boot',
        'Lavender Brown',
        ...
    ]
};

删除物品财产

delete Hogwarts.staff;

delete Hogwarts['staff'];

删除 array index

现在,如果你想移除一个员工或学生,程序有点不同,因为两种属性都是由自己组成的。

Hogwarts.staff.splice(3, 1);

Hogwarts.staff.splice(Hogwarts.staff.indexOf('Minerva McGonnagall') - 1, 1);

笔记

虽然你技术上可以使用删除一个序列,使用它会导致收到错误的结果,当打电话,例如,Hogwarts.staff.length 后来。

因此,在从对象中删除值时,首先要考虑您是否正在处理对象属性,或者您是否正在处理序列值,并根据此选择适当的策略。

如果你想尝试这个,你可以使用这个Fiddle作为一个起点。

在JavaScript中,有2种常见方法可以从对象中删除属性。

第一個可轉換的方法是使用 delete object.property 運營商。

第二种方法是不可改变的,因为它不改变原始对象,是引用对象破坏和传播合成: const {财产,...rest} = 对象。

这个帖子很古老,我觉得它非常有用,所以我决定分享我写的 unset 函数,如果其他人看到这个帖子,想想为什么它不那么简单,因为它在 PHP unset 函数。

写下这个新的 unset 函数的原因,是保持所有其他变量的指数在这个 hash_map. 查看下面的例子,看看如何在从 hash_map 中删除值后没有改变“test2”的指数。

function unset(unsetKey, unsetArr, resort) { var tempArr = unsetArr; var unsetArr = {}; delete tempArr[unsetKey]; if (resort) { j = -1; } for (i in tempArr) { if (typeof(tempArr[i]) !== 'undefined') { if (resort) { j++; } else { j = i; } unsetArr[j] = tempArr[i]; } } return unsetArr; } var unsetArr = ['test', 'deletedString', 'test2']; console.log(unset('1', unsetArr, true)); // output Object {0: "test", 1: "test2"} console.log(unset('1', unsetArr, false)); // output Object {0: "test", 2: "test2"}

我们可以使用

使用删除 object.property 使用删除 object[‘property’] 使用休息,删除多个属性

让 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*”, “regex1”: “^http://.*”, “regex2”: “^http://.*”, “regex3”: “^http://.*”, “regex4”: “^http://.*” }; 删除 myObject.regex; // 使用删除 object.property // 或删除 myObject(‘regex1’); // 使用删除 object(‘property’) const { regex2, regex3, regex4,...newMyObject } = myObject; console.log(n)

另一个替代方案是使用 Underscore.js 图书馆。

請注意, _.pick() 和 _.omit() 兩者都返回對象的副本,並且不直接修改原始對象。

参考: 链接 _.pick(对象, *keys)

返回对象的副本,过滤到只有值为白化密钥(或一系列有效的密钥)。

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

_.pick(myJSONObject, "ircEvent", "method");
=> {"ircEvent": "PRIVMSG", "method": "newURI"};

参考: 链接 _.omit(对象, * 关键)

返回对象的副本,过滤以忽略黑名单的钥匙(或一系列的钥匙)。

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

_.omit(myJSONObject, "regex");
=> {"ircEvent": "PRIVMSG", "method": "newURI"};

对于序列,可以类似的方式使用 _.filter() 和 _.reject()。