考虑到一个对象:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
如何删除财产 regex 以完成下一个 myObject?
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
考虑到一个对象:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
如何删除财产 regex 以完成下一个 myObject?
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
当前回答
在JavaScript中,有2种常见方法可以从对象中删除属性。
第一個可轉換的方法是使用 delete object.property 運營商。
第二种方法是不可改变的,因为它不改变原始对象,是引用对象破坏和传播合成: const {财产,...rest} = 对象。
其他回答
另一个替代方案是使用 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()。
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
要从对象中删除一个属性(指向对象),你可以这样做:
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 写了一篇关于他们博客的删除声明的令人难以置信的深入博客帖子,理解删除。
如果你想要一个新的对象,除了一些,所有原始的钥匙,你可以使用破坏。
使用对象破坏,一个ECMAScript 6功能,它是如此简单:
const { a, ...rest } = { a: 1, b: 2, c: 3 };
或与问题样本:
const myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
const { regex, ...newObject } = myObject;
console.log(newObject);
你可以在Babel try-out编辑中看到它。
编辑:
要重新分配到相同的变量,请使用:
let myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
({ regex, ...myObject } = myObject);
console.log(myObject);
有几种方法可以从物体中删除属性:
1) 使用点属性附件移除(可变)
2. 使用方形布拉克特属性配件移除(可转换)
const myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*”, }; delete myObject[‘regex’]; console.log(myObject); // or const name = ‘ircEvent’; delete myObject[name]; console.log(myObject);
3) 替代选项,但没有改变原始对象,使用对象破坏和休息合成(不变)
const myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*”, }; const { regex,...myObjectRest} = myObject; console.log(myObjectRest);