考虑到一个对象:

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

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

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

当前回答

下面是ES6如何轻松地删除输入:

让 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; const removeItem = “regex”; const { [removeItem]: remove,...rest } = myObject; console.log(remove); // “^http://.*” console.log(rest); // Object { ircEvent: “PRIVMSG”, 方法: “newURI” }

其他回答

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

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

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

您在查询标题中使用的术语,从JavaScript对象中删除一个属性,可以以不同的方式解释。 一个是将其删除整个内存和对象密钥列表或另一个只是将其从您的对象中删除。 正如在其他答案中提到的那样,删除关键词是主要部分。

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

如果你做:

console.log(Object.keys(myJSONObject));

结果将是:

["ircEvent", "method", "regex"]

delete myJSONObject["regex"];

["ircEvent", "method"]

但问题是,如果你关心记忆,你想完成的对象从记忆中被删除,建议在删除密钥之前将其设置为零:

myJSONObject["regex"] = null;
delete myJSONObject["regex"];

var regex = myJSONObject["regex"];

或者将其作为一个新的指标添加到另一个对象,如:

var myOtherObject = {};
myOtherObject["regex"] = myJSONObject["regex"];

delete regex; //False

结果将是虚假的,这意味着您的删除声明没有按照您的预期进行。但是,如果您以前没有创建这个变量,并且您只有 myOtherObject(“regex”)作为您的最后一个现有参考,您可能只是通过删除它:

myOtherObject["regex"] = null;
delete myOtherObject["regex"];

换句话说,一个JavaScript对象可以被杀,一旦在您的指向该对象的代码中没有留下任何参考。


更新:

谢谢 @AgentME:

更多关于 Object.seal 的信息: Object.seal()

您可以使用如下的过滤器。

var myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; // Way 1 let filter1 = {} Object.keys({...myObject}).filter(d => { if(d!=='regex'){ filter1[d] = myObject[d]; }) console.log(filter1) // Way 2 let filter2 = Object.fromEntries(Object.entries({...myObject}).filter(d => d[0]!=='regex' )) console.log(filter2)

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

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

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

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