考虑到一个对象:
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"
};
当前回答
const object = { prop1: 10, prop2: 20, prop3: 30, “test prop”: “This is test props” } console.log(object); // 将打印所有 4 props 删除 object.prop1; 删除 object[“test prop”]; console.log(object); // 将打印仅 prop2 和 prop3
其他回答
删除对象的两种方式
使用为... 在函数中删除用户(键) { const newUsers = {}; for (const uid in users) { if (uid!== key) { newUsers[uid] = users[uid]; } return newUsers }
或
delete users[key]
我们可以使用
使用删除 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)
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
另一个解决方案,使用 Array#reduce。
(六)
const myObject = { ircEvent: 'PRIVMSG',方法: 'newURI', regex: '^http://.*', }; const myNewObject = Object.keys(myObject).reduce((obj, key) => { key!=='regex'? obj[key] = myObject[key] : null; return obj; }, {}); console.log(myNewObject);
Object.assign() 和 Object.keys() 和 Array.map()
const obj = { “过滤器”:[ { “过滤器类型”:”之间”, “区域”:”BasicInformationRow.A0”, “MaxValue”:”2017-10-01”, “MinValue”:”2017-09-01”, “值”:”过滤器值” } } }; let new_obj1 = Object.assign({}, obj.Filters[0]); let new_obj2 = Object.assign({}, obj.Filters[0]); /* // old version let sh