给定一个对象:
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"
};
假设您有一个看起来像这样的对象:
var Hogwarts = {
staff : [
'Argus Filch',
'Filius Flitwick',
'Gilderoy Lockhart',
'Minerva McGonagall',
'Poppy Pomfrey',
...
],
students : [
'Hannah Abbott',
'Katie Bell',
'Susan Bones',
'Terry Boot',
'Lavender Brown',
...
]
};
如果你想使用整个staff
数组,正确的方式是这样做:
delete Hogwarts.staff;
或者,你也可以这样做:
delete Hogwarts['staff'];
同样地,如果要删除整个学生数组,可以调用 delete Hogwarts.students;
或 delete Hogwarts['students'];
。
如果您想删除单个教职员工或学生,则操作有所不同,因为两个属性本身都是数组。
如果您知道员工的索引,可以这样做:
Hogwarts.staff.splice(3, 1);
如果您不知道索引,您还需要进行索引搜索:
Hogwarts.staff.splice(Hogwarts.staff.indexOf('Minerva McGonnagall') - 1, 1);
虽然你可以在数组中使用 delete
,但这样做会导致稍后调用例如 Hogwarts.staff.length
时获得不正确的结果。换句话说,delete
将删除元素,但它不会更新 length
属性的值。使用 delete
也会破坏你的索引。
因此,在从对象中删除值时,始终首先考虑你是否正在处理对象属性或数组值,并根据此选择适当的策略。
如果想要尝试一下,请使用 这个Fiddle 作为起点。
delete myObject.regex;
// OR
delete myObject['regex'];
var Employee = {
age: 28,
name: 'Alireza',
designation: 'developer'
}
console.log(delete Employee.name); // returns true
console.log(delete Employee.age); // returns true
// When trying to delete a property that does
// not exist, true is returned
console.log(delete Employee.salary); // returns true
了解更多信息并查看更多示例,请访问以下链接:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
const myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*",
};
delete myObject.regex;
console.log(myObject);
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);
使用Array#reduce
方法也可以得到另一种解决方案。
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
myObject = Object.keys(myObject).reduce(function(obj, key) {
if (key != "regex") { //key you want to remove
obj[key] = myObject[key];
}
return obj;
}, {});
console.log(myObject);
然而,它将改变原始对象。如果你想创建一个没有指定键的新对象,只需将reduce函数分配给一个新变量,例如:
(ES6)
const myObject = {
ircEvent: 'PRIVMSG',
method: 'newURI',
regex: '^http://.*',
};
const myNewObject = Object.keys(myObject).reduce((obj, key) => {
key !== 'regex' ? obj[key] = myObject[key] : null;
return obj;
}, {});
console.log(myNewObject);
这里有很多好的答案,但我想补充一点,当使用 JavaScript 中的 delete 来删除属性时,通常最好先检查该属性是否存在以避免错误。
例如:
var obj = {"property":"value", "property2":"value"};
if (obj && obj.hasOwnProperty("property2")) {
delete obj.property2;
} else {
//error handling
}
由于 JavaScript 的动态性质,常常存在一些情况你并不确定属性是否存在。在使用 && 运算符之前检查 obj 是否存在还可以确保在未定义的对象上调用 hasOwnProperty() 函数时不会引发错误。这篇文章已经很旧了,但我认为它非常有用,所以我决定分享我写的unset函数,以防其他人看到这篇文章并认为为什么它不像PHP unset函数那样简单。
编写这个新的unset
函数的原因是要保留此哈希映射中所有其他变量的索引。请看以下示例,并查看从哈希映射中删除值后“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"}
使用ramda#dissoc,您将获得一个没有regex
属性的新对象:
const newObject = R.dissoc('regex', myObject);
// newObject !== myObject
您也可以使用其他函数来达到相同的效果 - 省略、选择等等...
尝试以下方法。将Object
属性值分配为undefined
。然后对对象进行stringify
和parse
。
var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
myObject.regex = undefined;
myObject = JSON.parse(JSON.stringify(myObject));
console.log(myObject);
import omit from 'lodash/omit';
const prevObject = {test: false, test2: true};
// Removes test2 key from previous object
const nextObject = omit(prevObject, 'test2');
R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}
_.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4});
对我来说不起作用,但是_.omit({a: 1, b: 2, c: 3, d: 4}, ['a', 'd']);
却可以。 - ー PupSoZeyDe ー